/**
 * DHTML date validation script for dd/mm/yyyy. 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 strDay=dtStr.substring(0,pos1)
	var strMonth=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 (pos1==-1 || pos2==-1){
		error1 = "Please enter your date of birth in\nthe following date format: dd/mm/yyyy"
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		error1 = "Please enter a valid month"
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		error1 = "Please enter a valid day"
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		error1 = "Please enter a valid 4 digit year between "+minYear+" and "+maxYear
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		error1 = ("Please enter a valid date")
		return false
	}
return true
}

function checkDate(dob){
	var dt=dob
	if (isDate(dt)==false){
		return false
	}
    return true
 }
 
function sendEntry(compID) {
	
	errormsg = ''
	
	if (!eval('document.comp' + compID + '.tnc.checked')) {
		errormsg = errormsg + ('Please check the box to confirm that you agree to the terms and conditions of entry\n')
	} 
	//if (eval('document.comp' + compID + '.loggedin.value') == "false" ) {
		//if (eval('document.comp' + compID + '.frm_username.value') == "") {
		//	errormsg = errormsg + 'Please enter your desired username\n'
		//}	
		//if (eval('document.comp' + compID + '.frm_pwd.value') == "") {
		//	errormsg = errormsg + 'Please enter a password\n'
		//}
		//if (eval('document.comp' + compID + '.frm_email.value') == "") {
		//	errormsg = errormsg + 'Please enter your email address\n'
		//}
		if (eval('document.comp' + compID + '.qName.value') == "") {
			errormsg = errormsg + 'Please enter your name\n'
		}
		if (eval('document.comp' + compID + '.qEmail.value') == "") {
			errormsg = errormsg + ('Please enter your Email address\n')
		}
		if (eval('document.comp' + compID + '.qTelephone.value') == "") {
			errormsg = errormsg + ('Please enter your Phone number\n')
		}
		//if (eval('document.comp' + compID + '.frm_dob.value') == "") {
		//	errormsg = errormsg + ('Please enter your date of birth\n')
		//} else {
		//	dob = eval('document.comp' + compID + '.frm_dob.value')
		//	if (!checkDate(dob)) {
		//		errormsg = errormsg + error1
		//	}
		//}
	//}
	if (errormsg != "") {
		alert(errormsg)
	}
	else {
		qString = "";
		numQuestions = eval('document.comp' + compID + '.numQuestions_' + compID + '.value');
		if (numQuestions > 0) {
			for (a=1;a<=numQuestions;a++){
				qType = eval('document.comp'	+ compID + '.qType_' + a + '.value');
				qID = eval('document.comp'		+ compID + '.qID_' + a + '.value');
				
				// radio (qtype=1),
				if (qType == 1) {
					numOptions = eval('document.comp' + compID + '.qNumOptions_' + a + '.value');
					oString = "";
					for (b=0;b<=numOptions-1;b++){
						if (eval('document.comp' + compID + '.q' + a + '['+b+'].checked')){
							oString = b+1;
						}
					}
					qString += "&q" + a + "=" + oString;
				}
				
				// multiple checkboxes (2)
				if (qType == 2) {
					numOptions = eval('document.comp' + compID + '.qNumOptions_' + a + '.value');
					oString = ""
					for (b=1;b<=numOptions;b++){
						oString = oString + eval('document.comp' + compID + '.q' + a + '_' + b + '.checked') + "^";
					}
					qString += "&q" + a + "=" +  oString
				}
				
				// droplist (3)
				if (qType == 3) {
					qString += "&q" + a + "=" +  eval('document.comp' + compID + '.q' + a + '.selectedIndex');
				}
				
				// multi droplist (4)
				if (qType == 4) {
					numOptions = eval('document.comp' + compID + '.qNumOptions_' + a + '.value');
					oString = ""
					for (b=0;b<numOptions;b++){
						oString = oString + eval('document.comp' + compID + '.q' + a + '[' + b + '].selected') + "^";
					}
					qString += "&q" + a + "=" +  oString
				}
				//  textbox (5), textarea (6)
				if (qType == 5 || qType == 6) {
					qString += "&q" + a + "=" + escape(eval('document.comp' + compID + '.q' + a + '.value'));
				}
				// single checkbox (7)
				if (qType == 7) {
					qString += "&q" + a + "=" +  eval('document.comp' + compID + '.q' + a + '.checked');
				}
				qString += "&qType_" + a + "=" + qType;
				qString += "&qOptions_" + a + "=" + eval('document.comp' + compID + '.qNumOptions_' + a + '.value');;
				qString += "&qID_" + a + "=" + qID;
			}
			// extra fields
			qString += "&qName="		+  escape(eval('document.comp' + compID + '.qName.value'));
			qString += "&qEmail="		+  escape(eval('document.comp' + compID + '.qEmail.value'));
			qString += "&qAddress="		+  escape(eval('document.comp' + compID + '.qAddress.value'));
			qString += "&qTelephone="	+  escape(eval('document.comp' + compID + '.qTelephone.value'));
			qString += "&qMobile="		+  escape(eval('document.comp' + compID + '.qMobile.value'));
			qString += "&qMarketing="	+  escape(eval('document.comp' + compID + '.qMarketing.value'));
			qString += "&qCount="		+  escape(eval('document.comp' + compID + '.numQuestions_' + compID + '.value'));
			qString += "&qUsername="	+  escape(eval('document.comp' + compID + '.frm_username.value'));
			qString += "&qPassword="	+  escape(eval('document.comp' + compID + '.frm_pwd.value'));
			qString += "&qDob="			+  escape(eval('document.comp' + compID + '.frm_dob.value'));
			qString += "&qEmailAdd="	+  escape(eval('document.comp' + compID + '.frm_email.value'));
		}
		curl = "compentry.asp?data=true&compId=" + compID + qString;
		window.location.href = curl;
	}
}
