/* FORM VALIDATION SCRIPT */

/***************************************************************************************************************/
/*	BIND ONLOAD PRELOADERING IMAGES FUNCTION		       		 		   									   */
/***************************************************************************************************************/
$(window).bind('load', function() {
    var preload = new Array();
    $(".hover").each(function() {
        s = $(this).attr("src").replace(/\.(.+)$/i, "_on.$1");
        preload.push(s)
    });
    var img = document.createElement('img');
    $(img).bind('load', function() {
        if(preload[0]) {
            this.src = preload.shift();
        }
    }).trigger('load');
});
/***************************************************************************************************************/

/***************************************************************************************************************/
/*	AJAX ERROR ALERT				   													       		 		   */
/***************************************************************************************************************/
$().ajaxError(function(ev,xhr,o,err) {
    alert(err);
    if (window.console && window.console.log) console.log(err);
});
/***************************************************************************************************************/

/***************************************************************************************************************/
/*	SET FORM VALIDATION ON DOCUMENT READY												       		 		   */
/***************************************************************************************************************/
$(document).ready(function(){
	$('#frmNEWSLETTER').ajaxForm( { beforeSubmit: doNEWSLETTERVALIDATE, success: doNEWSLETTERRESPONSE, url: 'ajax-action.php?action=newsletter-signup&id=1', resetForm: true } );
	$('#frmCONTACTUS').ajaxForm( { beforeSubmit: doCONTACTUSVALIDATE, success: doCONTACTUSRESPONSE, url: 'ajax-action.php?action=contactus&id=2', resetForm: true } );	
});
/***************************************************************************************************************/

/***************************************************************************************************************/
/*	NEWSLETTER FORM VALIDATION													   						 		   */
/***************************************************************************************************************/
function doNEWSLETTERVALIDATE(){
	var theForm = document.frmNEWSLETTER;
	if(Trim(theForm.txtNEWSLETTER_NAME.value).length == 0){
		alert("Please enter your full name.");
		theForm.txtNEWSLETTER_NAME.focus();
		return false;
	}
	if(Trim(theForm.txtNEWSLETTER_EMAIL.value).length == 0){
		alert("Please enter your email address.");
		theForm.txtNEWSLETTER_EMAIL.focus();
		return false;
	}
	if(isEmail(theForm.txtNEWSLETTER_EMAIL) == false) {
		alert("Please enter your valid email address.\nFor Example: xyz@xyz.com");
		theForm.txtNEWSLETTER_EMAIL.focus();
		return false;
	}
	if(Trim(theForm.txtNEWSLETTER_CAPTCHA.value).length == 0){
		alert("Please enter verification code.");
		theForm.txtNEWSLETTER_CAPTCHA.focus();
		return false;
	}
	doAJAXLOADER('load');
	doOVERLAPBACKGROUND('load');
}
/***************************************************************************************************************/

/***************************************************************************************************************/
/*	NEWSLETTER RESPONSE													   						 		   	    */
/***************************************************************************************************************/
function doNEWSLETTERRESPONSE(responseXML){		
	var theForm = document.frmNEWSLETTER;
	var newsletter_nodes = responseXML.getElementsByTagName('response');	
	if(newsletter_nodes.length > 0){
		signedup = newsletter_nodes[0].getElementsByTagName("newslettersignup").item(0).firstChild.data.toString();
		failedreason = newsletter_nodes[0].getElementsByTagName("failedreason").item(0).firstChild.data.toString();
		doAJAXLOADER('unload');
		doOVERLAPBACKGROUND('unload');		
		if(signedup == "yes"){			
			alert("You have successfully signuped for our newsletter.");
			return false;
		}		
		if(signedup == "no"){
			if(failedreason == "1"){
				alert("Your entered email id is already in our database.");
				return false;			
			}
			if(failedreason == "2"){
				alert("You have entered invalid verification code.\nPlease try again.");
				return false;
				window.location.reload();
			}			
		}
	}else{		
		alert("There was an error occurred.\nPlease try again or contact administrator.");
		theForm.txtNEWSLETTER_NAME.focus();
		return false;
	}
}
/***************************************************************************************************************/

/***************************************************************************************************************/
/*	CONTACT US FORM VALIDATION													   						 	   */
/***************************************************************************************************************/
function doCONTACTUSVALIDATE(){
	var theForm = document.frmCONTACTUS;
	if(Trim(theForm.txtFULL_NAME.value).length == 0){
		alert("Please enter your name.");
		theForm.txtFULL_NAME.focus();
		return false;
	}
	if(Trim(theForm.txtADDRESS_1.value).length == 0){
		alert("Please enter your address (line 1).");
		theForm.txtADDRESS_1.focus();
		return false;
	}
	if(Trim(theForm.txtCITY_NAME.value).length == 0){
		alert("Please enter your city name.");
		theForm.txtCITY_NAME.focus();
		return false;
	}
	if(Trim(theForm.txtSTATE_NAME.value).length == 0){
		alert("Please enter your state name.");
		theForm.txtSTATE_NAME.focus();
		return false;
	}
	if(Trim(theForm.txtPIN_CODE.value).length == 0){
		alert("Please enter your pin/zip code.");
		theForm.txtPIN_CODE.focus();
		return false;
	}
	if(theForm.cmbCOUNTRY_NAME.selectedIndex <= 0){
		alert("Please select your country name.");
		theForm.cmbCOUNTRY_NAME.focus();
		return false;
	}
	if(Trim(theForm.txtPHONE_NO.value).length == 0){
		alert("Please enter your phone no.");
		theForm.txtPHONE_NO.focus();
		return false;
	}
	if(Trim(theForm.txtEMAIL_ADDRESS.value).length == 0){
		alert("Please enter your email address.");
		theForm.txtEMAIL_ADDRESS.focus();
		return false;
	}
	if(isEmail(theForm.txtEMAIL_ADDRESS) == false) {
		alert("Please enter your valid email address.\nFor Example: xyz@xyz.com");
		theForm.txtEMAIL_ADDRESS.focus();
		return false;
	}
	if(theForm.cmbREGARDING.selectedIndex <= 0){
		alert("Please enter select your purpose of contact.");
		theForm.cmbREGARDING.focus();
		return false;
	}
	if(Trim(theForm.txaADDITIONAL_MESSAGE.value).length > 3000){
		alert("Please enter only 3000 characters\nin additional message.");
		theForm.txaADDITIONAL_MESSAGE.focus();
		return false;
	}
	if(Trim(theForm.txtCONTACTUS_CAPTCHA.value).length == 0){
		alert("Please enter verification code.");
		theForm.txtCONTACTUS_CAPTCHA.focus();
		return false;
	}
	doAJAXLOADER('load');
	doOVERLAPBACKGROUND('load');
}
/***************************************************************************************************************/

/***************************************************************************************************************/
/*	CONTACT US RESPONSE													   						 		   	   */
/***************************************************************************************************************/
function doCONTACTUSRESPONSE(responseXML){
	var theForm = document.frmCONTACTUS;
	var contactus_nodes = responseXML.getElementsByTagName('response');	
	if(contactus_nodes.length > 0){
		fsubmitted = contactus_nodes[0].getElementsByTagName("feedbacksubmitted").item(0).firstChild.data.toString();
		failedreason = contactus_nodes[0].getElementsByTagName("failedreason").item(0).firstChild.data.toString();
		doAJAXLOADER('unload');
		doOVERLAPBACKGROUND('unload');		
		if(fsubmitted == "yes"){			
			alert("Thank you very much for your feedback.\n\nWe'll review your feedback, comments, and address any issue\nthat there may be, as soon as possible.");
			return false;
		}		
		if(fsubmitted == "no"){
			if(failedreason == "1"){
				alert("You have entered invalid verification code.\nPlease try again.");
				return false;
				window.location.reload();
			}			
		}
	}else{		
		alert("There was an error occurred.\nPlease try again or contact administrator.");
		theForm.txtFULL_NAME.focus();
		return false;
	}
}
/***************************************************************************************************************/

/***************************************************************************************************************/
/*	REGISTRATION FORM STEP #1 VALIDATION													   				   */
/***************************************************************************************************************/
function doREGISTRATIONS1VALIDATE(){
	var theForm = document.frmREGISTRATION_S1;
	if(Trim(theForm.txtDELEGATE_NAME.value).length == 0){
		alert("Please enter name of delegate.");
		theForm.txtDELEGATE_NAME.focus();
		return false;
	}
	if(theForm.cmbDELEGATE_CATEGORY.selectedIndex <= 0){
		alert("Please select delegate's registration category.");
		theForm.cmbDELEGATE_CATEGORY.focus();
		return false;
	}
	if(Trim(theForm.txtDELEGATE_AGE.value).length == 0){
		alert("Please enter delegate's age.");
		theForm.txtDELEGATE_AGE.focus();
		return false;
	}
	if(isNumeric(theForm.txtDELEGATE_AGE) == false) {
		alert("Please enter delegate's valid age.");
		theForm.txtDELEGATE_AGE.focus();
		return false;
	}
	if(theForm.cmbDELEGATE_GENDER.selectedIndex <= 0){
		alert("Please select delegate's sex.");
		theForm.cmbDELEGATE_GENDER.focus();
		return false;
	}
	if(theForm.cmbDELEGATE_NATIONALITY.selectedIndex <= 0){
		alert("Please select delegate's nationality.");
		theForm.cmbDELEGATE_NATIONALITY.focus();
		return false;
	}
	if(Trim(theForm.flDELEGATE_PHOTOGRAPH.value).length > 0){
		checkExt = theForm.flDELEGATE_PHOTOGRAPH.value.toLowerCase();
		if((checkExt.indexOf('.jpg') == -1) && (checkExt.indexOf('.jpeg') == -1) && (checkExt.indexOf('.gif') == -1)){ 
			alert("Please select one of below mentioned file types\nfor delegate's photograph:\n\n1) .jpg\n2) .jpeg\n3) .gif"); 
			theForm.flDELEGATE_PHOTOGRAPH.focus();
			return false;	
		}
	}	
	if(Trim(theForm.txtADDRESS_1.value).length == 0){
		alert("Please enter delegate's corresponding address (line 1).");
		theForm.txtADDRESS_1.focus();
		return false;
	}
	if(Trim(theForm.txtCITY_NAME.value).length == 0){
		alert("Please enter delegate's corresponding city name.");
		theForm.txtCITY_NAME.focus();
		return false;
	}
	if(Trim(theForm.txtSTATE_NAME.value).length == 0){
		alert("Please enter delegate's corresponding state name.");
		theForm.txtSTATE_NAME.focus();
		return false;
	}
	if(Trim(theForm.txtPIN_CODE.value).length == 0){
		alert("Please enter delegate's corresponding pin/zip code.");
		theForm.txtPIN_CODE.focus();
		return false;
	}
	if(theForm.cmbCOUNTRY_NAME.selectedIndex <= 0){
		alert("Please select delegate's corresponding country name.");
		theForm.cmbCOUNTRY_NAME.focus();
		return false;
	}
	if(Trim(theForm.txtPHONE_NO.value).length == 0){
		alert("Please enter delegate's corresponding phone no.");
		theForm.txtPHONE_NO.focus();
		return false;
	}
	if(Trim(theForm.txtEMAIL_ADDRESS.value).length == 0){
		alert("Please enter delegate's corresponding email address.");
		theForm.txtEMAIL_ADDRESS.focus();
		return false;
	}
	if(isEmail(theForm.txtEMAIL_ADDRESS) == false) {
		alert("Please enter delegate's valid corresponding email address.\nFor Example: xyz@xyz.com");
		theForm.txtEMAIL_ADDRESS.focus();
		return false;
	}
	if(Trim(theForm.flDELEGATE_PHOTOGRAPH.value).length > 0){
		theForm.hidPHOTOGRAPH_P.value = "true";
	}	
	theForm.hidACTION.value = "SubmitRegistrationFormStep1";
	doAJAXLOADER('load');
	doOVERLAPBACKGROUND('load');
	theForm.action = "formaction.php";
	return true;
}
/***************************************************************************************************************/

/***************************************************************************************************************/
/*	REGISTRATION FORM STEP #2 VALIDATION													   				   */
/***************************************************************************************************************/
function doACCREGISTRATIONVALIDATE(){
	var theForm = document.frmREGISTRATION_S2;
	var acc_person_1 = false;
	var acc_person_2 = false;
	var acc_person_3 = false;
	acc_per_1_name = Trim(theForm.txtACC_PERSON_1_NAME.value).length;
	acc_per_1_category = theForm.cmbACC_PERSON_1_CATEGORY.selectedIndex;
	acc_per_1_age = Trim(theForm.txtACC_PERSON_1_AGE.value).length;
	acc_per_1_gender = theForm.cmbACC_PERSON_1_GENDER.selectedIndex;
	acc_per_1_nationality = theForm.cmbACC_PERSON_1_NATIONALITY.selectedIndex;
	acc_per_1_photograph = Trim(theForm.flACC_PERSON_1_PHOTOGRAPH.value).length;
	acc_per_2_name = Trim(theForm.txtACC_PERSON_2_NAME.value).length;
	acc_per_2_category = theForm.cmbACC_PERSON_2_CATEGORY.selectedIndex;
	acc_per_2_age = Trim(theForm.txtACC_PERSON_2_AGE.value).length;
	acc_per_2_gender = theForm.cmbACC_PERSON_2_GENDER.selectedIndex;
	acc_per_2_nationality = theForm.cmbACC_PERSON_2_NATIONALITY.selectedIndex;
	acc_per_2_photograph = Trim(theForm.flACC_PERSON_2_PHOTOGRAPH.value).length;
	acc_per_3_name = Trim(theForm.txtACC_PERSON_3_NAME.value).length;
	acc_per_3_category = theForm.cmbACC_PERSON_3_CATEGORY.selectedIndex;
	acc_per_3_age = Trim(theForm.txtACC_PERSON_3_AGE.value).length;
	acc_per_3_gender = theForm.cmbACC_PERSON_3_GENDER.selectedIndex;
	acc_per_3_nationality = theForm.cmbACC_PERSON_3_NATIONALITY.selectedIndex;
	acc_per_3_photograph = Trim(theForm.flACC_PERSON_3_PHOTOGRAPH.value).length;
	
	if((acc_per_1_name > 0) || (acc_per_1_category > 0) || (acc_per_1_age > 0) || (acc_per_1_gender > 0) || (acc_per_1_nationality > 0) || (acc_per_1_photograph > 0)){
		acc_person_1 = true;
		if(acc_per_1_name <= 0){
			alert("Please enter accompanying person(#1)'s name.");
			theForm.txtACC_PERSON_1_NAME.focus();
			return false;
		}
		if(acc_per_1_category <= 0){
			alert("Please select accompanying person(#1)'s registration category.");
			theForm.cmbACC_PERSON_1_CATEGORY.focus();
			return false;
		}
		if(acc_per_1_age <= 0){
			alert("Please enter accompanying person(#1)'s age.");
			theForm.txtACC_PERSON_1_AGE.focus();
			return false;
		}
		if(acc_per_1_age > 0){
			if(isNumeric(theForm.txtACC_PERSON_1_AGE) == false) {
				alert("Please enter accompanying person(#1)'s valid age.");
				theForm.txtACC_PERSON_1_AGE.focus();
				return false;
			}
		}
		if(acc_per_1_gender <= 0){
			alert("Please select accompanying person(#1)'s gender.");
			theForm.cmbACC_PERSON_1_GENDER.focus();
			return false;
		}
		if(acc_per_1_nationality <= 0){
			alert("Please select accompanying person(#1)'s nationality.");
			theForm.cmbACC_PERSON_1_NATIONALITY.focus();
			return false;
		}
		if(acc_per_1_photograph > 0){
			checkExt = theForm.flACC_PERSON_1_PHOTOGRAPH.value.toLowerCase();
			if((checkExt.indexOf('.jpg') == -1) && (checkExt.indexOf('.jpeg') == -1) && (checkExt.indexOf('.gif') == -1)){ 
				alert("Please select one of below mentioned file types\nfor accompanying person(#1)'s photograph:\n\n1) .jpg\n2) .jpeg\n3) .gif"); 
				theForm.flACC_PERSON_1_PHOTOGRAPH.focus();
				return false;	
			}
			theForm.hidPHOTOGRAPH_P1.value = "true";
		}
	}
	if((acc_per_2_name > 0) || (acc_per_2_category > 0) || (acc_per_2_age > 0) || (acc_per_2_gender > 0) || (acc_per_2_nationality > 0) || (acc_per_2_photograph > 0)){
		acc_person_2 = true;
		if(acc_per_2_name <= 0){
			alert("Please enter accompanying person(#2)'s name.");
			theForm.txtACC_PERSON_2_NAME.focus();
			return false;
		}
		if(acc_per_2_category <= 0){
			alert("Please select accompanying person(#2)'s registration category.");
			theForm.cmbACC_PERSON_2_CATEGORY.focus();
			return false;
		}
		if(acc_per_2_age <= 0){
			alert("Please enter accompanying person(#2)'s age.");
			theForm.txtACC_PERSON_2_AGE.focus();
			return false;
		}
		if(acc_per_2_age > 0){
			if(isNumeric(theForm.txtACC_PERSON_2_AGE) == false) {
				alert("Please enter accompanying person(#2)'s valid age.");
				theForm.txtACC_PERSON_2_AGE.focus();
				return false;
			}
		}
		if(acc_per_2_gender <= 0){
			alert("Please select accompanying person(#2)'s gender.");
			theForm.cmbACC_PERSON_2_GENDER.focus();
			return false;
		}
		if(acc_per_2_nationality <= 0){
			alert("Please select accompanying person(#2)'s nationality.");
			theForm.cmbACC_PERSON_2_NATIONALITY.focus();
			return false;
		}
		if(acc_per_2_photograph > 0){
			checkExt = theForm.flACC_PERSON_2_PHOTOGRAPH.value.toLowerCase();
			if((checkExt.indexOf('.jpg') == -1) && (checkExt.indexOf('.jpeg') == -1) && (checkExt.indexOf('.gif') == -1)){ 
				alert("Please select one of below mentioned file types\nfor accompanying person(#2)'s photograph:\n\n1) .jpg\n2) .jpeg\n3) .gif"); 
				theForm.flACC_PERSON_2_PHOTOGRAPH.focus();
				return false;	
			}
			theForm.hidPHOTOGRAPH_P2.value = "true";
		}
	}
	if((acc_per_3_name > 0) || (acc_per_3_category > 0) || (acc_per_3_age > 0) || (acc_per_3_gender > 0) || (acc_per_3_nationality > 0) || (acc_per_3_photograph > 0)){
		acc_person_3 = true;
		if(acc_per_3_name <= 0){
			alert("Please enter accompanying person(#3)'s name.");
			theForm.txtACC_PERSON_3_NAME.focus();
			return false;
		}
		if(acc_per_3_category <= 0){
			alert("Please select accompanying person(#3)'s registration category.");
			theForm.cmbACC_PERSON_3_CATEGORY.focus();
			return false;
		}
		if(acc_per_3_age <= 0){
			alert("Please enter accompanying person(#3)'s age.");
			theForm.txtACC_PERSON_3_AGE.focus();
			return false;
		}
		if(acc_per_3_age > 0){
			if(isNumeric(theForm.txtACC_PERSON_3_AGE) == false) {
				alert("Please enter accompanying person(#3)'s valid age.");
				theForm.txtACC_PERSON_3_AGE.focus();
				return false;
			}
		}
		if(acc_per_3_gender <= 0){
			alert("Please select accompanying person(#3)'s gender.");
			theForm.cmbACC_PERSON_3_GENDER.focus();
			return false;
		}
		if(acc_per_3_nationality <= 0){
			alert("Please select accompanying person(#3)'s nationality.");
			theForm.cmbACC_PERSON_3_NATIONALITY.focus();
			return false;
		}
		if(acc_per_3_photograph > 0){
			checkExt = theForm.flACC_PERSON_3_PHOTOGRAPH.value.toLowerCase();
			if((checkExt.indexOf('.jpg') == -1) && (checkExt.indexOf('.jpeg') == -1) && (checkExt.indexOf('.gif') == -1)){ 
				alert("Please select one of below mentioned file types\nfor accompanying person(#3)'s photograph:\n\n1) .jpg\n2) .jpeg\n3) .gif"); 
				theForm.flACC_PERSON_3_PHOTOGRAPH.focus();
				return false;	
			}
			theForm.hidPHOTOGRAPH_P3.value = "true";
		}
	}
	if((acc_person_1 == true) || (acc_person_2 == true) || (acc_person_3 == true)){
		theForm.hidACTION.value = "SubmitRegistrationFormStep2";
		doAJAXLOADER('load');
		doOVERLAPBACKGROUND('load');
		theForm.action = "formaction.php";
		return true;
	}else{
		if(!confirm("You're not provided any information related to accompanying persons.\nAre you sure to skip this step?")){				
			return false;
		}else{
			doAJAXLOADER('load');
			doOVERLAPBACKGROUND('load');
			window.location.href = "e-registration-payment.php";
			return false;
		}		
	}	
}
/***************************************************************************************************************/

/***************************************************************************************************************/
/*	REGISTRATION FORM SKIP STEP #2	    													   				   */
/***************************************************************************************************************/
function doSKIPREGISTRATIONSTEP2(){
	doAJAXLOADER('load');
	doOVERLAPBACKGROUND('load');
	window.location.href = "e-registration-payment.php";
	return false;
}
/***************************************************************************************************************/

/***************************************************************************************************************/
/*	CHECK PAYMENT OPTION SET BY DELEGATE   													   				   */
/***************************************************************************************************************/
function doPAYMENTPROCESSING(){
	var theForm = document.frmPAYMENTPROCESSING;	
	if(theForm.rdoPAYMENT_OPTION[0].checked == true){
		theForm.hidACTION.value = "PaymentProcessingViaCCAvenue";
		doAJAXLOADER('load');
		doOVERLAPBACKGROUND('load');
		theForm.action = "payment-processing.php";
		return true;
	}
	if(theForm.rdoPAYMENT_OPTION[1].checked == true){
		theForm.hidACTION.value = "RegistrationConfirmationWithOfflinePayment";
		doAJAXLOADER('load');
		doOVERLAPBACKGROUND('load');
		theForm.action = "e-registration-offline-response.php";
		return true;
	}
}
/***************************************************************************************************************/