//////////////////////////////////////////////////////////////
//                                                          //
//  This file requires the inclusion of /common/ls_lib.js   //
//                                                          //
//////////////////////////////////////////////////////////////

//added 111209 begin
/**
 * 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;
var mdy=0;
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(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	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 (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		mdy='m'
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		mdy='d'
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		mdy='y'
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}
//added 111209 end


function validateForm(form) {



    var password;
    var count=0;
    var i=0;
    var k=0;
    var j=0;
    var p=0;
        
    var keywords = new Array('OR', 'AND', 'HAVING', 'UNION', 'DROP', 'ALTER', 'UPDATE', 'DELETE', 'INSERT');
    var invalidChars = new Array('\u0027', '\b', '\t','\v', '\f', '\n', '\r', '\u0020', '\'' );
 
    password=(form.password.value).toUpperCase();
    p = password.length;

    // E-mail
    if(form.email) {
        if(isEmpty(form.email.value) || !isValidEmail(form.email.value)) {
            alert('You must supply a valid email address.');
            form.email.focus();
            form.email.select();
            return false;
        }
    }

    // Password
    if(form.password) {
        if(isEmpty(form.password.value)) {
            alert('You must supply a password.');
            form.password.focus();
            return false;
        }
        if(form.password_confirm && (form.password_confirm.value != form.password.value)) {
            alert('Your passwords do not match. Please re-enter your passord.');
            form.password_confirm.value = '';
            form.password.value = '';
            form.password.focus();
            return false;
        }

	for (i=0; i<keywords.length; i++)
   	{
       	   k=password.indexOf(keywords[i]);
       
       	   //if we found an invalid keyword in memberid increment counter 
          if (k != -1) count++;
   	}
   
   	for (m=0; m<invalidChars.length; m++)
   	{
       	   j=password.indexOf(invalidChars[m]);
       
           //if we found an invalid keyword in memberid increment counter 
           if (m != -1) j++;
        }
   
  
       
   	if (count != 0 && j !=0)
   	{
           //alert('Not a valid password. invalid words:' + count + 'invalid chars: ' + j);
           alert('Not a valid password.');
           //reset counter
           count=0;
           form.password.focus();
           return false;
        }

    }



    // First name
    if(form.first) {
        if(isEmpty(form.first.value)) {
            alert('You must supply a first name.');
            form.first.focus();
            return false;
        }
    }
    // Last name
    if(form.last) {
        if(isEmpty(form.last.value)) {
            alert('You must supply a last name.');
            form.last.focus();
            return false;
        }
    }


	// DATE OF BIRTH (DAY)
	if(form.birthday != null) {
		if(form.birthday.selectedIndex == 0) { // first item selected
		
			alert('You must supply day of birth.');
			form.birthday.focus();
			return false;
		}
	}
	
	// DATE OF BIRTH (MONTH)
	if(form.birthmonth != null) {
		if(form.birthmonth.selectedIndex == 0) { // first item selected
		
			alert('You must supply month of birth.');
			form.birthmonth.focus();
			return false;
		}
	}

	// DATE OF BIRTH (PLAYER UPDATE)
	if(form.birthdate != null) {
		if(form.birthdate.selectedIndex == 0) { // first item selected
		
			alert('Please enter birthdate.');
			form.birthdate.focus();
			return false;
		}
	}	

    // birth year
	if(form.yyyy != null) {
		form.yyyy.value = Trim(form.yyyy.value);
		str = form.yyyy.value;
		if(str == '') {
			
			alert('Please enter birthday year.');
			form.yyyy.focus();
			return false;
		}
		if(!isAlphanumeric(str, '0123456789')) {

            alert('Please enter numeric value in birth year.');
			form.yyyy.focus();

			return false;    
		}
		if(str.length!=4) {

            alert('Please enter birth year in 4 digit.');
			form.yyyy.focus();

			return false;    
		}
		var now = new Date();
		if( ((eval(str)+21) > now.getFullYear() ) ) {

            alert('You have to be above 21 years to register.');
			form.yyyy.focus();

			return false;    
		}
		if( ((eval(str)+125) < now.getFullYear() ) ) {

            alert('Please enter your correct year of birth.');
			form.yyyy.focus();

			return false;    
		}
		
	}

    //birthdate validation
	if(form.yyyy != null) {

	    var dt1=form.birthmonth.value;
	    var dt2=form.birthday.value;
	    var dt3=form.yyyy.value;
	    var dt=dt1 + "/" + dt2 + "/" + dt3;

	    if (isDate(dt)==false){
	        if (mdy=='m') form.birthmonth.focus();
	        if (mdy=='d') form.birthday.focus();
	        if (mdy=='y') form.yyyy.focus();
		    return false
	    }
              var dateToCheck=new Date ( dt3, dt1-1, dt2 )
              var eighteenYearsAgo = new Date();
              eighteenYearsAgo.setYear(eighteenYearsAgo.getFullYear() - 21);
              eighteenYearsAgo.setHours(0);
              eighteenYearsAgo.setMinutes(0);
              eighteenYearsAgo.setSeconds(1);

              if (dateToCheck.getTime() > eighteenYearsAgo.getTime())
              {
                alert("You must be at least 21 years of age.");
                form.yyyy.focus();
                //objField.select();
                return false;
              }            
     }

    // company
    if(form.company) {
        if(isEmpty(form.company.value)) {
            alert('You must supply the company name.');
            form.company.focus();
            return false;
        }
    }


    // Address
    if(form.address) {
        if(isEmpty(form.address.value)) {
            alert('You must supply an address.');
            form.address.focus();
            return false;
        }
    }

    // City
    if(form.city) {
        if(isEmpty(form.city.value)) {
            alert('You must supply a City.');
            form.city.focus();
            return false;
        }
    }

	// State/Province
    if(form.state) {
    	if(form.state.selectedIndex == 0) { // first item selected
            alert('You must select a State/Province.');
            form.state.focus();
            return false;
        }
    }

    // Country
    if(form.country) {
        if(form.country.selectedIndex == 0) { // first item selected
            alert('You must select a Country.');
            form.country.focus();
            return false;
        }
    }

    // Postal/Zip Code (only mandatory for US/Canada
    if(form.zip) {
        var country = '';
        if(form.country)
            country = form.country.options[form.country.selectedIndex].text;

        var zip = form.zip.value + '';
        if(country == 'United States' || country == 'Canada') {
            if(isEmpty(zip)) {
                alert('You must supply a Zip/Postal Code.');
                form.zip.focus();
                return false;
            }
            if (country.toUpperCase() == 'UNITED STATES' && !(/^\d{5}$/.test(zip) || /^\d{5}-\d{4}$/.test(zip))) {
                alert('You have entered an invalid US Zip code\nPlease use the one of the following formats:'
                    + '\n99999 or 99999-9999');
                form.zip.focus();
                form.zip.select();
                return false;
            }
            if (country.toUpperCase() == 'CANADA' && !(/^\D{1}\d{1}\D{1}\-?\d{1}\D{1}\d{1}$/.test(zip))) {
                alert('You have entered an invalid Canadian Postal Code\nPlease use the one of the following formats:'
                   + '\nZ9Z-9Z9 or Z9Z9Z9');
                form.zip.focus();
                form.zip.select();
                return false;
            }
        }
    }

    // Phone
    if(form.phone) {
        if(isEmpty(form.phone.value) || form.phone.value.length < 10) {
            alert('You must enter a valid phone number.');
            form.phone.focus();
            form.phone.select();
            return false;
        }
    	if(!bContainsValidChars(form.phone.value, numchars + '+ .-')) {
            alert('You have entered an invalid phone number.\nAllowable characters include only the following:\n  digits [0-9]\n  hyphens [-]\n  spaces [ ]\n  periods [.]');
            form.phone.focus();
            form.phone.select();
            return false;
        }
    }
    // Paytype
    if(form.payMethod) {
        if(form.payMethod.options[form.payMethod.selectedIndex].text == 'Please Make A Selection') {
            alert('You must select a Preferred Payment Method');
            form.payMethod.focus();
            return false;
        }
    }
    
        // agree
    if(form.Agree) {
        if(form.Agree.checked == false) {
            alert('You must go through the agreement.');
            return false;
        }
    }

    
    return true;
}