function LTrim(VALUE){
    var w_space = String.fromCharCode(32);
    if(v_length < 1){
        return"";
    }
    var v_length = VALUE.length;
    var strTemp = "";

    var iTemp = 0;

    while(iTemp < v_length){
        if(VALUE.charAt(iTemp) == w_space){
        }
        else{
            strTemp = VALUE.substring(iTemp,v_length);
            break;
        }
        iTemp = iTemp + 1;
    } //End While
    return strTemp;
} //End Function

function RTrim(VALUE){
    var w_space = String.fromCharCode(32);
    var v_length = VALUE.length;
    var strTemp = "";
    if(v_length < 0){
        return"";
    }
    var iTemp = v_length -1;

    while(iTemp > -1){
        if(VALUE.charAt(iTemp) == w_space){
        }
        else{
            strTemp = VALUE.substring(0,iTemp +1);
            break;
        }
        iTemp = iTemp-1;

    } //End While
    return strTemp;

} //End Function


function CheckEmpty(formField){
    if(formField.value=="" || formField.value==null || formField.value==0){
        return true;
    }
    else{
        return false;
    }
}

function Trim(TRIM_VALUE){
    if(TRIM_VALUE.length < 1){
        return "";
    }
    TRIM_VALUE = RTrim(TRIM_VALUE);
    TRIM_VALUE = LTrim(TRIM_VALUE);
		
    if(TRIM_VALUE==""){
        return "";
    }
    else{
        return TRIM_VALUE;
    }
}

function checkEmailID(emailID){
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailID)){
        return (true)
    }
	
    return (false)
}

function ZipChar(string){
    var iChars = "!@#$%^&*()+=[]\\\';,./{}|\":<>?";

    for (var i = 0; i < string.length; i++) {
        if (iChars.indexOf(string.charAt(i)) != -1) {

            return false;
        }
    }
    return true;
}

function hasSpecialChar(string){
    var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";

    for (var i = 0; i < string.length; i++) {
        if (iChars.indexOf(string.charAt(i)) != -1) {

            return false;
        }
    }
    return true;
}

function validateValue( strValue, strMatchPattern ) {
    /************************************************
DESCRIPTION: Validates that a string a matches
  a valid regular expression value.
    
PARAMETERS:
   strValue - String to be tested for validity
   strMatchPattern - String containing a valid
      regular expression match pattern.
      
RETURNS:
   True if valid, otherwise false.
*************************************************/
    var objRegExp = new RegExp( strMatchPattern);
 
    //check if string matches pattern
    return objRegExp.test(strValue);
}

function echeck(s) {
    if (isCharsInBag(s, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.-@") == false)
    {
        return false;
    }
    if(s.charAt(0) == '@' || s.charAt(0) == '.')
    {
        return false;
    }
    else if(s.charAt(s.length-1) == '@' || s.charAt(s.length-1) == '.')
    {
        return false;
    }
    else if(s.indexOf('@.') != -1 || s.indexOf('.@') != -1 || s.indexOf('..') != -1)
    {
        return false;
    }
		 
    var i;
    var found1=false;
    var found2=false;
    var count1=0;
    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        if(s.charAt(i)== ' ') {
            found1=true;found2=true;count1=2;
        }
        if(s.charAt(i)== '@') {
            found1=true;count1++;
        }
        if(s.charAt(i)== '.') {
            found2=true;
        }
    }
    if(found1==true && found2==true)
    {
        if(count1 > 1)
            return false;
        else
            return true;
    }
    else
    {
        return false;
    }
    return true;
}

function ismaxlength(obj){
    var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
    if (obj.getAttribute && obj.value.length>mlength)
        obj.value=obj.value.substring(0,mlength)
}




function isValidURL(url){ 
    var arr = new Array('.com','.net','.org','.biz','.coop','.info','.museum','.name','.pro','.edu','.gov','.int','.mil','.ac','.ad','.ae','.af','.ag','.ai','.al','.am','.an','.ao','.aq','.ar','.as','.at','.au','.aw','.az','.ba','.bb','.bd','.be','.bf','.bg','.bh','.bi','.bj','.bm','.bn','.bo','.br','.bs','.bt','.bv','.bw','.by','.bz','.ca','.cc','.cd','.cf','.cg','.ch','.ci','.ck','.cl','.cm','.cn','.co','.cr','.cu','.cv','.cx','.cy','.cz','.de','.dj','.dk','.dm','.do','.dz','.ec','.ee','.eg','.eh','.er','.es','.et','.fi','.fj','.fk','.fm','.fo','.fr','.ga','.gd','.ge','.gf','.gg','.gh','.gi','.gl','.gm','.gn','.gp','.gq','.gr','.gs','.gt','.gu','.gv','.gy','.hk','.hm','.hn','.hr','.ht','.hu','.id','.ie','.il','.im','.in','.io','.iq','.ir','.is','.it','.je','.jm','.jo','.jp','.ke','.kg','.kh','.ki','.km','.kn','.kp','.kr','.kw','.ky','.kz','.la','.lb','.lc','.li','.lk','.lr','.ls','.lt','.lu','.lv','.ly','.ma','.mc','.md','.mg', '.mh','.mk','.ml','.mm','.mn','.mo','.mp','.mq','.mr','.ms','.mt', '.mu','.mv','.mw','.mx','.my','.mz','.na','.nc','.ne','.nf','.ng', '.ni','.nl','.no','.np','.nr','.nu','.nz','.om','.pa','.pe','.pf', '.pg','.ph','.pk','.pl','.pm','.pn','.pr','.ps','.pt','.pw','.py', '.qa','.re','.ro','.rw','.ru','.sa','.sb','.sc','.sd','.se','.sg', '.sh','.si','.sj','.sk','.sl','.sm','.sn','.so','.sr','.st','.sv', '.sy','.sz','.tc','.td','.tf','.tg','.th','.tj','.tk','.tm','.tn', '.to','.tp','.tr','.tt','.tv','.tw','.tz','.ua','.ug','.uk','.um', '.us','.uy','.uz','.va','.vc','.ve','.vg','.vi','.vn','.vu','.ws', '.wf','.ye','.yt','.yu','.za','.zm','.zw');
    var mai = url;
    var val = true;
    var dot = mai.lastIndexOf(".");
    var dname = mai.substring(0,dot);
    var ext = mai.substring(dot,mai.length);
    if(dot>=0) {
        for(var i=0; i<arr.length; i++)  {
            if(ext == arr[i])    {
                val = true;   break;
            }else{
                val = false;
            }
        }
        if(val == false){
            return false;
        }else {
            return true;
        }
    }else{
        return false;
    }
} 

function isValidURL2(url){ 
    var urlRegxp = /^(http:\/\/www.|https:\/\/www.|www.){1}([\w]+)(.[\w]+){1,2}$/;
    if (urlRegxp.test(url)){
        //alert("valid");
        return true;
    } else {
        //alert("invalid website");
        return false;
    }
    
} 

function isValidURLWithHttp(url){ 
    var urlRegxp = /^(http:\/\/|https:\/\/){1}([\w]+)(.[\w]+){1,2}/;
    if (urlRegxp.test(url)){
        //alert("valid");
        return true;
    } else {
        //alert("invalid website");
        return false;
    }
    
} 

function isValidDomain(url){ 
    var urlRegxp = /^(www.){1}([\w]+)(.[\w]+){1,2}$/;
    if (urlRegxp.test(url)){
        //alert("valid");
        return true;
    } else {
        //alert("invalid website");
        return false;
    }
    
} 

function checkDomain(nname)
{
    var arr = new Array(
        '.com','.net','.org','.biz','.coop','.info','.museum','.name',
        '.pro','.edu','.gov','.int','.mil','.ac','.ad','.ae','.af','.ag',
        '.ai','.al','.am','.an','.ao','.aq','.ar','.as','.at','.au','.aw',
        '.az','.ba','.bb','.bd','.be','.bf','.bg','.bh','.bi','.bj','.bm',
        '.bn','.bo','.br','.bs','.bt','.bv','.bw','.by','.bz','.ca','.cc',
        '.cd','.cf','.cg','.ch','.ci','.ck','.cl','.cm','.cn','.co','.cr',
        '.cu','.cv','.cx','.cy','.cz','.de','.dj','.dk','.dm','.do','.dz',
        '.ec','.ee','.eg','.eh','.er','.es','.et','.fi','.fj','.fk','.fm',
        '.fo','.fr','.ga','.gd','.ge','.gf','.gg','.gh','.gi','.gl','.gm',
        '.gn','.gp','.gq','.gr','.gs','.gt','.gu','.gv','.gy','.hk','.hm',
        '.hn','.hr','.ht','.hu','.id','.ie','.il','.im','.in','.io','.iq',
        '.ir','.is','.it','.je','.jm','.jo','.jp','.ke','.kg','.kh','.ki',
        '.km','.kn','.kp','.kr','.kw','.ky','.kz','.la','.lb','.lc','.li',
        '.lk','.lr','.ls','.lt','.lu','.lv','.ly','.ma','.mc','.md','.mg',
        '.mh','.mk','.ml','.mm','.mn','.mo','.mp','.mq','.mr','.ms','.mt',
        '.mu','.mv','.mw','.mx','.my','.mz','.na','.nc','.ne','.nf','.ng',
        '.ni','.nl','.no','.np','.nr','.nu','.nz','.om','.pa','.pe','.pf',
        '.pg','.ph','.pk','.pl','.pm','.pn','.pr','.ps','.pt','.pw','.py',
        '.qa','.re','.ro','.rw','.ru','.sa','.sb','.sc','.sd','.se','.sg',
        '.sh','.si','.sj','.sk','.sl','.sm','.sn','.so','.sr','.st','.sv',
        '.sy','.sz','.tc','.td','.tf','.tg','.th','.tj','.tk','.tm','.tn',
        '.to','.tp','.tr','.tt','.tv','.tw','.tz','.ua','.ug','.uk','.um',
        '.us','.uy','.uz','.va','.vc','.ve','.vg','.vi','.vn','.vu','.ws',
        '.wf','.ye','.yt','.yu','.za','.zm','.zw');

    var mai = nname;
    var val = true;

    var dot = mai.lastIndexOf(".");
    var dname = mai.substring(0,dot);
    var ext = mai.substring(dot,mai.length);
    //alert(ext);
	
    if(dot>2 && dot<57)
    {
        for(var i=0; i<arr.length; i++)
        {
            if(ext == arr[i])
            {
                val = true;
                break;
            }
            else
            {
                val = false;
            }
        }
        if(val == false)
        {
            alert("Your domain extension "+ext+" is not correct");
            return false;
        }
        else
        {
            for(var j=0; j<dname.length; j++)
            {
                var dh = dname.charAt(j);
                var hh = dh.charCodeAt(0);
                if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123) || hh==45 || hh==46)
                {
                    if((j==0 || j==dname.length-1) && hh == 45)
                    {
                        //alert("Domain name should not begin are end with '-'");
                        return false;
                    }
                }
                else{
                    //alert("Your domain name should not have special characters");
                    return false;
                }
            }
        }
    }else{
        //alert("Your Domain name is too short/long");
        return false;
    }
    return true;
}

function isCharsInBag (s, bag)
{
    var i;
    // Search through string's characters one by one.
    // If character is in bag, append to returnString.
 
    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) return false;
    }
    return true;
}



function isZIP(s)
{
    if (isCharsInBag (s, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") == false)
    {
        return false;
    }
    return true;
}


function isPhone(s)
{

    if(s.length < 5 || s.length > 16)
        return false;
    if (isCharsInBag (s, "- +()0123456789") == false)
    {
        return false;
    }
    if(s.indexOf('+') > 0)
    {
        return false;
    }
    return true;
}

function isPhoneNew(s)
{
    if(s.length < 5 || s.length > 16)
        return false;
    if (isCharsInBag (s, "0123456789") == false)
    {
        return false;
    }
    return true;
}

function CheckFieldLength(field,charcount,cntfield,maxlimit) {
	
    if (field.value.length > maxlimit){ // if too long...trim it!
        field.value        = field.value.substring(0, maxlimit);
    }
	
    document.getElementById(cntfield).innerHTML = maxlimit - field.value.length;
}


function setCounter(field,cntfield,maxlimit) {
    document.getElementById(cntfield).innerHTML = maxlimit - field.value.length;
}

function isEmail(s)
{
    if (isCharsInBag(s, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.-@") == false)
    {
        return false;
    }
    if(s.charAt(0) == '@' || s.charAt(0) == '.')
    {
        return false;
    }
    else if(s.charAt(s.length-1) == '@' || s.charAt(s.length-1) == '.')
    {
        return false;
    }
    else if(s.indexOf('@.') != -1 || s.indexOf('.@') != -1 || s.indexOf('..') != -1)
    {
        return false;
    }
 
    var i;
    var found1=false;
    var found2=false;
    var count1=0;
    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        if(s.charAt(i)== ' ') {
            found1=true;found2=true;count1=2;
        }
        if(s.charAt(i)== '@') {
            found1=true;count1++;
        }
        if(s.charAt(i)== '.') {
            found2=true;
        }
    }
    if(found1==true && found2==true)
    {
        if(count1 > 1)
            return false;
        else
            return true;
    }
    else
    {
        return false;
    }
    return true;
}

function numberonly(e,dvalue){
    var checkdot=dvalue.indexOf(".");
    var unicode; //=e.charCode? e.charCode : e.keyCode
			
    if( !e ) {
        if( window.event ) {
            e = window.event;
        }else {}
    }
  
    if(e.keyCode) {
        unicode = e.keyCode;
    } else if(e.which) {
        unicode = e.which;
    } else if(e.charCode) {
        unicode = e.charCode;
    } else {}
	
    if(unicode == 46 && checkdot >0){
        return false;
    }
    if(unicode!=46){
        if(unicode!=8 && unicode!=13 && unicode!=9 && unicode!=39 && unicode!=37){
            if(unicode < 48 || unicode > 57){
                return false;
            }
        }
    }
}
 
function numberWithoutDecimal(e,dvalue){
	
    var unicode; //=e.charCode? e.charCode : e.keyCode
			
    if( !e ) {
        if( window.event ) {
            e = window.event;
        }else {}
    }
  
    if(e.keyCode) {
        unicode = e.keyCode;
    } else if(e.which) {
        unicode = e.which;
    } else if(e.charCode) {
        unicode = e.charCode;
    } else {}
	
    if(unicode!=8 && unicode!=13 && unicode!=9 && unicode!=39 && unicode!=37){
        if(unicode < 48 || unicode > 57){
            return false;
        }
    }
	
}


/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "-";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
    var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
    for (var i = 1; i <= n; i++) {
        this[i] = 31
        if (i==4 || i==6 || i==9 || i==11) {
            this[i] = 30
        }
        if (i==2) {
            this[i] = 29
        }
    }
    return this
}

function isDate(dtStr){
    var daysInMonth = DaysArray(12)
	
    var pos1=dtStr.indexOf(dtCh)
    var pos2=dtStr.indexOf(dtCh,pos1+1)
	
    var strMonth=dtStr.substring(pos1+1,pos1+3)
    var strDay=dtStr.substring(pos2+1)
    var strYear=dtStr.substring(0,pos1)

    strYr=strYear
    if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
    if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
    for (var i = 1; i <= 3; i++) {
        if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
    }
    month=parseInt(strMonth)
    day=parseInt(strDay)
    year=parseInt(strYr)
    if (pos1==-1 || pos2==-1){
        alert("The date format should be : yyyy-mm-dd")
        return false
    }
    if(isNaN(month)){
        alert("Please enter a valid month")
        return false
    }
    if (strMonth.length<1 || month<1 || month>12){
        alert("Please enter a valid month")
        return false
    }
    if(isNaN(day)){
        alert("Please enter a valid day")
        return false
    }
    if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
        alert("Please enter a valid day")
        return false
    }
    if (strYear.length != 4 || year==0 || year<minYear){
        alert("Please enter a valid 4 digit year")
        return false
    }
    if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
        alert("Please enter a valid date")
        return false
    }
    return true
}

function chktab(e){
    var unicode; //=e.charCode? e.charCode : e.keyCode
			
    if( !e ) {
        if( window.event ) {
            e = window.event;
        }else {}
    }
  
    if(e.keyCode) {
        unicode = e.keyCode;
    } else if(e.which) {
        unicode = e.which;
    } else if(e.charCode) {
        unicode = e.charCode;
    } else {}
    if(unicode==13 || unicode==9){
        return true;
    }else{
        return false;
    }
}


function checkuncheckall(){

    total = 0;
    checkboxlen = 0;
    checkbox = false;
    len = document.newsletterListing.elements.length
    for(i=0;i<len;i++){
        if((document.newsletterListing.elements[i].type=='checkbox')&&(document.newsletterListing.elements[i].name.indexOf('box_')==0)){
            checkbox = true;
            checkboxlen = checkboxlen + 1;
            if(document.newsletterListing.elements[i].checked==true){
                total = total + 1;
            }
        }
    }
    if(checkbox == true){
        if(checkboxlen==total){
            document.getElementById("chkall").checked = true;
        }else{
            document.getElementById("chkall").checked = false;
        }
    }
}


function selectdate(value){
    if(value=="between"){
        document.getElementById('divfrm').style.display="block";
    }
    if(value=="7" || value=="30" || value=="0"){
        document.getElementById('divfrm').style.display="none";
    }
}


function numbersonly(e){
    var unicode=e.charCode? e.charCode : e.keyCode;
    if (unicode!=8){ //if the key isn't the backspace key (which we should allow)
        if (unicode<48||unicode>57 ) //if not a number
            return false //disable key press
    }
}

function IsNumeric(sText)

{
    var ValidChars = "0123456789.";
    var IsNumber=true;
    var Char;


    for (i = 0; i < sText.length && IsNumber == true; i++)
    {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1)
        {
            IsNumber = false;
        }
    }
    return IsNumber;

}


function load_image(imgpath)
{
    if(imgpath != "")
    {
        // code to get File Extension..
        var arr1 = new Array;

        arr1 = imgpath.split("\\");
        var len = arr1.length;
        var img1 = arr1[len-1];
        var filext = img1.substring(img1.lastIndexOf(".")+1);

        // Checking Extension

        if(filext == "jpg" || filext == "jpeg" || filext == "gif" || filext == "bmp" || filext == "png")
            return true;
        else
        {
            return false;
        }
    }
    return true;
}

function load_mp3(imgpath)
{
    if(imgpath != "")
    {
        // code to get File Extension..
        var arr1 = new Array;

        arr1 = imgpath.split("\\");
        var len = arr1.length;
        var img1 = arr1[len-1];
        var filext = img1.substring(img1.lastIndexOf(".")+1);

        // Checking Extension

        if(filext == "mp3")
            return true;
        else
        {
            return false;
        }
    }
    return true;
}

/*FUNCTION: validate_youtube_url
Returns: array(valid URI:Boolean, Error Message or Video ID:String, match results:Array)
*/

function validate_youtube_url(str, protocol){
   if(!protocol && protocol != ''){
       protocol = '(http://www.)|(www.)';
   }
   protocol = protocol.replace(/\//g, '\/', protocol).replace(/\./g, '\.');
   protocol = (protocol != '') ? '^(' + protocol + ')' : protocol;
   match_exp = new RegExp(protocol + 'youtube\.com\/(.+)(v=.+)', 'gi');
   
   var matches = match_exp.exec(str);
   if(matches!= null){
       if(matches.length < 3){
           return Array(false, 'Invalid URI', matches);
       }else{
           var qs = matches[matches.length-1].split('&');
           var vid = false;
           for(i=0; i<qs.length; i++){
               var x = qs[i].split('=');
               if(x[0] == 'v' && x[1]){
                   vid = x[1];
                   return Array(true, vid, matches);
               }else{
                   return Array(false, 'Missing ID', matches);
               }
           }
           return Array(false, false, false);
       }
   }else{
       return false;
   }
}

/*FUNCTION: validate_vimeo_url
Returns: array(valid URI:Boolean, Error Message or Video ID:String, match results:Array)
*/
function validate_vimeo_url(str, protocol){
   if(!protocol && protocol != ''){
       protocol = '(http://www.)|(www.)';
   }
   
   protocol = protocol.replace(/\//g, '\/', protocol).replace(/\./g, '\.');
   protocol = (protocol != '') ? '^(' + protocol + ')' : protocol;
   match_exp = new RegExp(protocol + 'vimeo\.com\/', 'gi');

   var matches = match_exp.exec(str);

    if(matches!= null){
       if(matches.length < 3){
           return Array(false, 'Invalid URI', matches);
       }else{           
            return Array(true, true, matches);
       }
           return false;
       }
}
