﻿
function limitText(limitField, limitNum) {
    limitField = $(limitField);
	if (limitField.val().length > limitNum) {
		limitField.val(limitField.val().substring(0, limitNum));
	} else {
		//limitField.val(limitNum - limitField.val().length);
	}
}

function IsNumeric(strString) //  check for valid numeric strings	
{
 	if(!/\D/.test(strString)) return true;//IF NUMBER
 	else if(/^\d+\.\d+$/.test(strString)) return true;//IF A DECIMAL NUMBER HAVING AN INTEGER ON EITHER SIDE OF THE DOT(.)
 	else return false;
}

function pageWidth() {return $(window).width();}
function pageHeight() {return $(window).height();}

function PopUpWindowWidth()
{
    var Width = pageWidth();
    if(Width < 700){Width = 700;}//force a Minimum size
    return Width;
}

function PopUpWindowHeight()
{
    var Height = pageHeight();
    if(Height < 600){Height = 600;}//force a Minimum size
    return Height;
}

function RefreshPage()
{
    setTimeout("window.location.href = window.location.href;", 500);//needs a timeout because it may call AjaxRequestString
}

function isValidEmail(strEmailAddress)//single
{
    strEmailAddress = strEmailAddress.toUpperCase(); //Reg Exp looks at case
    var reg = new RegExp("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$");
    if (reg.test(strEmailAddress.trim()) && strEmailAddress.indexOf("'") == -1)//aso check for a single quote(couldnt find a regExp that was complete)
    {
        return true;
    }
    else {
        return false;
    }
}

function isValidEmails(strEmails)//Multiple - uses isValidEmail
{
    var boolValidEmails = true;
    var emails = strEmails.split(",");
    for (i = 0; i < emails.length; i++) {
        if (!isValidEmail(emails[i])) {
            boolValidEmails = false;
            break;
        }
    }
    return boolValidEmails;
}

function IsCreditCardValid(sCardNumber, sCCCompany) {
    var blnIsValid = false;
    if (luhnAlgorithm(sCardNumber)) {
        var reg;
        switch (sCCCompany) {
            case 'American Express': //American Express
                reg = new RegExp("^3[47][0-9]{13}$");
                break;
            case 'Discover': //Discover/Novus
                reg = new RegExp("^6(?:011|5[0-9]{2})[0-9]{12}$");
                break;
            case 'Master Card': //Master Card
                reg = new RegExp("^5[1-5][0-9]{14}$");
                break;
            case 'Visa': //VISA
                reg = new RegExp("^4[0-9]{12}(?:[0-9]{3})?$");
                break;
            default:  //not specified or supported - use the ALL card RegExp
                reg = new RegExp("^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$");
        }
        if (reg.test(sCardNumber)) {
            blnIsValid = true;
        }
    }
    return blnIsValid;
}

function luhnAlgorithm(sCardNumber) {
    var bDoubleDigit = false;
    var iSum = 0, iNum;

    for (var i = sCardNumber.length - 1; i >= 0; i--) {
        iNum = parseInt(sCardNumber.charAt(i));
        if (bDoubleDigit) {
            iSum += (iNum > 4) ? (iNum * 2 - 9) : (iNum * 2);
        } else {
            iSum += iNum;
        }
        bDoubleDigit = !bDoubleDigit;
    }
    return (iSum % 10) == 0;
}

function IsExpirationDateValid(sMonth, sYear) {
    var d = new Date();
    var curr_year = parseInt(d.getFullYear().toString().slice(2));
    var curr_month = d.getMonth() + 1;

    //month entered has to be equal or greater than current month -and- year entered must be equal or greater than current year
    //if month and year are 99 then allow it as that is what the DM sends back to the UI after it has the proper date
    if ((sMonth == 99 && sYear == 99) || (sMonth > 0 && sMonth < 13 && (sMonth >= curr_month || sYear != curr_year)) && (sYear >= curr_year)) {
        return true;
    }
    else {
        return false;
    }
}

String.prototype.trim = function () {
    return this.replace(/^\s*|\s*$/,"");
}

String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function Close()
{
   GetRadWindow().close();
}
function GetRadWindow()   
{
    var oWindow = null;   
    if (window.radWindow)
        oWindow = window.radWindow; //Will work in Moz in all cases, including clasic dialog   
    else if (window.frameElement.radWindow)
        oWindow = window.frameElement.radWindow;//IE (and Moz az well)   
    return oWindow;   
}
