/**
* @ Title:					regn/newregn.js
* @ Version:			1.0
* @ Copyright:		Copyright (c) 2004
* @ Author:			Deepali
* @ Company:		Avenues (I) Pvt. Limited.
* @ Description:	JavaScript file for new registration validations.
* @ Created:		7-May-04
* @ Modified:		31-Mar-05[deepali-Account scheme displayed on firstpage based on country selected +
*									 validation for accType inserted in goNext()]	
*									21-May-05[Deepali - Validation for bhutan and nepal(INR should be included in currency list)]
*/

function listAccType(){
	var cntryId = document.theForm.country.options[document.theForm.country.options.selectedIndex].value;
	var sch = document.theForm.schemeData1.value;
	var schData = sch.split('~');
	
	/* If country selected is India then the two schemes premium and economic would be populated
		 else popular
	*/
	document.theForm.accType.length = 1;	
	//alert("Call file newregn.js");
	for(var i =0; i<schData.length-2; i++){
		//if(cntryId == 'IN' || cntryId == 'NP' || cntryId == 'BT'){
			if(schData[i].substring(0, schData[i].indexOf("#")) != 'POP'){
				addOption(document.theForm.accType, schData[i].substring(schData[i].indexOf("#")+1), schData[i].substring(0, schData[i].indexOf("#")));
			}
/*		}else{
			if(schData[i].substring(0, schData[i].indexOf("#")) == 'POP'){
				addOption(document.theForm.accType, schData[i].substring(schData[i].indexOf("#")+1), schData[i].substring(0, schData[i].indexOf("#")));
			}
		}*/
	}
/*
	if(cntryId == 'IN' || cntryId == 'NP' || cntryId == 'BT'){
		for(var i =0; i<schData.length; i++){
			if(schData[i].substring(0, schData[i].indexOf("#")) != 'POP'){
				addOption(document.theForm.accType, schData[i], schData[i].substring(0, 3));				
			}
		}
	}else{
		for(var i =0; i<schData.length; i++){
			if(schData[i].substring(0, schData[i].indexOf("#")) == 'POP'){
				addOption(document.theForm.accType, schData[i], schData[i].substring(0, 3));				
				break;
			}
		}
	}	*/
	return true;
}

function updateOptions(regionName){
	
	var regionObj = getRegionDataObject(regionName);
	document.theForm.country.length = 0;
	var cont = new Array();
	cont = getCountryObject(regionObj);
	for(var k=0; k < cont.length; k++){
		addOption(document.theForm.country, cont[k+1], cont[k]);
		k=k+1;
	 }
	if(isMacintosh()&&isMSIE())document.theForm.country.blur();
}

function getRegionDataObject(regionName) {		
	var reg = document.theForm.regionData1.value;
	var regionData = reg.split('~');
	var regionObj = new Object();
	regionObj.id = -1;
	regionObj.name = "";
	regionName = regionName+'';	
	if (isEmpty(regionName)) return regionObj;
	
	var getdata = false;
	var index=0;
	for (var i=0;i<regionData.length;i++) {
			index=i; getdata = true;
	
	if (getdata) {
		var test = regionData[index+1];
		var t1 = test.substring(0, test.indexOf(','));
		var t2 = test.substring(test.indexOf(',')+1);
		if (regionName==t2) {
				regionObj.id = t1
				regionObj.name = t2;
				break;
			}	
		}
	}
	return regionObj;	
}

function getCountryObject(regionObj) {
	var con= document.theForm.countryData1.value;
	var countryData = con.split('~');
	var countryObj = new Object();
	countryObj.id = -1;
	countryObj.name = "";
	countryObj.countryCode = -1;
	countryObj.countryISOCode = "";
	if (isEmpty(regionObj.id) || isNaN(regionObj.id)) return countryObj;
	
	var index=0;
	var getdata = false;
	var con = new Array();
	var j = 0;
	for (var i=0;i<countryData.length;i++) {
		index = i; getdata = true; 
		if (getdata) {
			var test = countryData[index];
			var regId = test.substring(test.lastIndexOf(',')+1);
		
			if (regionObj.id == regId){
				var t1 = test.substring(0,test.indexOf(','));
				var t2 = test.substring(0, test.lastIndexOf(','));		
				var id = t2.substring(t2.lastIndexOf(',')+1);
				con[j] = id;
				con[++j] = t1;
				j++;
			}
		}
	}
	return con;
}

function addOption(cbox, name, value) {
	var len = cbox.length;
	var option = new Option();
	option.text = name;
	option.value = value;
	cbox.options[len] = option;
}

function isMSIE() {
	if ( (navigator.appVersion.indexOf ("MSIE") != -1) ) {
		return true;
	} 
	else {
		return false;
	}
}

function isMacintosh() {
	if ( (navigator.appVersion.indexOf ("Macintosh") != -1) ) {
		return true;
	} 
	else {
		return false;
	}
}

function isEmpty(str)	{
	var b = false;
		
	strTemp = new String(str);	
	if (strTemp == null)
		b = true;
	else
		if (strTemp == 'undefined' || strTemp=='null'|| strTemp.length == 0) b = true;
	
	return b;
}

function goNext(){
	
	if(!chkBlank(document.theForm.txtpropnum, "Number of properties")) return false;
	if(!chkNaN(document.theForm.txtpropnum, "Number of properties")) return false;

	var con_obj = document.theForm.country;

	if(con_obj.length == 0){
		alert("Choose a region");
		return false;
	}	

	if(document.theForm.country.options.selectedIndex < 0){
		alert("Choose a country");
		document.theForm.country.focus();
		return false;
	}

	if(document.theForm.accType.options.selectedIndex <= 0){
		if(document.theForm.accType.length <= 1){
			alert("The ResAvenue Account Scheme are not currently defined for the country selected. \n Try for someother region/country.");
			return false;
		}else{
			alert("Choose Account Type");
			document.theForm.accType.focus();
			return false;
		}
	}

//	document.theForm.cntry.value = document.theForm.country.options[document.theForm.country.options.selectedIndex].value;
document.theForm.actionPage.value = "/content/regn/registration2.jsp";
//	document.theForm.actionPage.value = "/regn/regn_regpage.jsp";
}


function getSetupList1(ele, disp){
	var currSelected = ele.options[ele.options.selectedIndex].value;
	if(currSelected == ""){
		alert("Select One");
		return;
	}
	var setup = "";
	var curArr = document.reg_frm.setup.value.split("^");
	for(var a=1;a<curArr.length;a++){		
		if(currSelected == curArr[a].substring(0, curArr[a].indexOf("#"))){
			setup = curArr[a];
			break;
		}
	}
	
	curArr = setup.split("|");
	var feeListObj = disp;	
	feeListObj.length = 1;
	feeListObj.options[1] = new Option("Select One", "");
	
	for(var a=0;a<curArr.length;a++){
		var curOpts = curArr[a].split("#");
		feeListObj.options[a+1] = new Option(curOpts[2], curOpts[1]);
	}	
	return false;
}
/*
 * new method added by narendra to validate password
 */



function validatePassword(password){
	 
	// alert("method called " + password.length );
	   var flag=false; 
	   if(password.length>7 && password.length<=20 ){
	     
	   var re = /^.*(?=.{8,})(?=.*\d)(?=.*[a-z])((?=.*[@#$%^&+=]).*$)/;
		if (!re.test(password)) { 
			alert("Password Should be alphanumeric with at least one special character ([@#$%^&+=])"); 
			flag= false;
		}
		else{
			flag= true;
		//	  alert("inside of  else special charcter " );
		}
		//flag= true;
	   }
		else if(password.length<=7){
		alert('The password should not be less than 8 characters');
		flag= false;
		
	   }
	   else if(password.length>20){
		alert('The password should not be grater than 20 characters');
		flag= false;
	   }
	   else {
		   flag= true;
	   }
	  //alert("flag is " +  flag);
   if(flag)
	   return true;
	else 
		return false;
	   
	   return flag; 
}
function reg_validate(){
	var cnt = document.reg_frm.startCnt.value;
	var listProp = document.reg_frm.listProp.value;

	//Check for registration details
	if(parseInt(cnt) < parseInt(listProp)){
		if(!chkBlank(document.reg_frm.fname, "First Name")) return false;
		if(!chkAlpha(document.reg_frm.fname, "First Name")) return false;
		
		if(document.reg_frm.mname.value != ''){
			if(!chkAlpha(document.reg_frm.mname, "Middle Name")) return false;
		}
		
		if(!chkBlank(document.reg_frm.lname, "Last Name")) return false;
		if(!chkAlpha(document.reg_frm.lname, "Last Name")) return false;

		if(!chkBlank(document.reg_frm.desgn, "Designation")) return false;
		if(!chkBlank(document.reg_frm.contEmail, "Contact Email")) return false;
		if(!chkEmail(document.reg_frm.contEmail, "Contact Email")) return false;

		//Check for phone numbers
		if(!chkBlank(document.reg_frm.phone1, "Phone Country Code")) return false;
		if(!chkNaN(document.reg_frm.phone1, "Phone Country Code")) return false;
		if(!chkBlank(document.reg_frm.phone2, "Phone Area Code")) return false;
		if(!chkNaN(document.reg_frm.phone2, "Phone Area Code")) return false;
		if(!chkBlank(document.reg_frm.phone3, "Phone Number")) return false;
		if(!chkNaN(document.reg_frm.phone3, "Phone Number")) return false;

		//Check for username logins and passwords
		if(!chkBlank(document.reg_frm.uname, "User Name")) return false;
		if(!chkSpecialChar(document.reg_frm.uname, "User Name")) return false;
		if(!chkBlank(document.reg_frm.upass, "Password")) return false;
		if(!chkUserPass(document.reg_frm.uname, document.reg_frm.upass)) return false;
		if(!chkBlank(document.reg_frm.repass, "Re type password")) return false;
		if(!chkPassword(document.reg_frm.upass, document.reg_frm.repass)) return false;	
		if(!validatePassword(document.reg_frm.upass.value)) return false;
		//Check for group details
		if(document.reg_frm.mNoOfProps.value >1){
			if(!chkBlank(document.reg_frm.grpName, "Group Name")) return false;
			if(!chkBlank(document.reg_frm.grpOwner, "Group Owner")) return false;
			if(!chkBlank(document.reg_frm.grpDesc, "Description")) return false;
			if(!chkListBlank(document.reg_frm.bizStat, "Business Filing Status")) return false;			
			if(!validateURL(document.reg_frm.grpURL, "Website URL")) return false;
		}
		//Check for property details
		if(!chkBlank(document.reg_frm.propname, "Property Name")) return false;
		if(!chkBlank(document.reg_frm.propaddr, "Property Address")) return false;
		if(!chkBlank(document.reg_frm.propcity, "Property City")) return false;
		if(!chkListBlank(document.reg_frm.proproomCurr, "Currency for Avg. Room Rate")) return false;	
		if(!chkListBlank(document.reg_frm.propavgRoomRate, "Avg. Room Rate")) return false;	
		if(!chkNaN(document.reg_frm.propRooms, "Number for Rooms")) return false;
		if(!chkLEZero(document.reg_frm.propRooms, "Number for Rooms to book online")) return false;
		if(!chkListBlank(document.reg_frm.propcountry, "Country")) return false;	

		if(!chkBlank(document.reg_frm.propzip, "Zip Code")) return false;
		if(!chkBlank(document.reg_frm.phone1, "Phone Country Code")) return false;

		if(!chkBlank(document.reg_frm.propEmail, "Email")) return false;
		if(!chkEmail(document.reg_frm.propEmail, "Email")) return false;

		//Check for business status if single property and payables and payouts at
		if(document.reg_frm.mNoOfProps.value < 2){
			if(!chkListBlank(document.reg_frm.bizStat, "Business Filing Status")) return false;			
	//				if(!chkListBlank(document.reg_frm.acct_type, "Account Type")) return false;	
			if(!validateURL(document.reg_frm.propURL, "Website URL")) return false;		
	//				if(!chkRadio(document.reg_frm.radio0, "Monthly Charges Payable By")) return false;
	//				if(!chkRadio(document.reg_frm.radioP0, "Payout at")) return false;
		}	

		//Checks if state is populated on selection of country
		if(document.reg_frm.propState.length > 1){
			if(document.reg_frm.propState.options.selectedIndex < 1){
				alert("Select State");
				document.reg_frm.propState.focus();
				return false;
			}
		}
	}

	if(parseInt(cnt) > 0){
		var i = 0;
		var d = parseInt(cnt) + 1;

		if(d == listProp){
				i = parseInt(cnt) - (parseInt(cnt) % parseInt(listProp)) + 1;
		}else{
			   i = parseInt(cnt) - (parseInt(cnt) % parseInt(listProp)); //+ 1;
		}

		for(; i <= parseInt(cnt); i++){
				var prop = eval("document.reg_frm.propname"+i);
				if(!chkBlank(prop, "Property Name")) return false;
				var cy = eval("document.reg_frm.propcity"+i);
				if(!chkBlank(cy, "City")) return false;
				if(!chkSpecialChar(cy, "City")) return false;
				if(!chkListBlank(eval("document.reg_frm.propcountry"+i), "Country")) return false;	
				if(!chkListBlank(eval("document.reg_frm.proproomCurr"+i), "Currency for Avg. Room Rate")) return false;	
				if(!chkListBlank(eval("document.reg_frm.propavgRoomRate"+i), "Avg. Room Rate")) return false;	
				if(!chkRadio(eval("document.reg_frm.radio"+i), "Monthly Charges Payable By")) return false;
				if(!chkRadio(eval("document.reg_frm.radioP"+i), "Payout at")) return false;
		}
	}
	//		alert(" transcurr : "+ document.reg_frm.transCurr.options.selectedIndex);
	if(document.reg_frm.transCurr.options.selectedIndex < 0){
		alert("Select Processing Currencies");
		return false;
	}
	document.reg_frm.actionPage.value = "/servlet/regn.servNewRegnSubmit";
}


function getStates(sid){ 
	document.reg_frm.country.selectedIndex = 1;
	changeStateCombo(document.reg_frm.country, document.reg_frm.propState);
	for(var s = 0; s < document.reg_frm.propState.length;s++){
		if(document.reg_frm.propState.options[s].value == sid){
			document.reg_frm.propState.selectedIndex = s;
			break;
		}
	}	
}

function currList(){
	document.reg_frm.actionPage.value = "/regn/PG_getBankDetails.jsp";
	document.reg_frm.submit();
}

function listCurr(num){
	//	var cnt = document.reg_frm.mNoOfProps.value;			
		var cnt = document.reg_frm.startCnt.value;			
		var listProp = document.reg_frm.listProp.value;
		
		if(listProp == 1){
			cnt = 0;
		}

		var india_selected = false;
		var propObj = null;
		if (parseInt(cnt) < parseInt(listProp)){
			if(num == 1){
					changeStateCombo(document.reg_frm.propcountry, document.reg_frm.propState);
			}
			if(document.reg_frm.propcountry.options.selectedIndex > 0){
//				alert(document.reg_frm.propcountry.options[document.reg_frm.propcountry.options.selectedIndex].value);
				if(document.reg_frm.propcountry.options[document.reg_frm.propcountry.options.selectedIndex].value == 'IN' || document.reg_frm.propcountry.options[document.reg_frm.propcountry.options.selectedIndex].value == 'NP' || document.reg_frm.propcountry.options[document.reg_frm.propcountry.options.selectedIndex].value == 'BT'){
						 india_selected = true;
				}else{
						 india_selected = false;
				}
			}
		}

		if(cnt > 0){
				var i = 0;
				var d = parseInt(cnt) + 1;
				if(d == listProp){
					i = cnt - (cnt % listProp) + 1;
				}else{
				   i = cnt - (cnt % listProp) ;//+ 1;
				}

		
				if(!india_selected){
					for(; i <= cnt; i++){
						propObj = eval("document.reg_frm.propcountry"+ i);
						if(propObj.options.selectedIndex > 0){
							if (propObj.options[propObj.options.selectedIndex].value == 'IN' || propObj.options[propObj.options.selectedIndex].value == 'BT' || propObj.options[propObj.options.selectedIndex].value == 'NP'){
							//alert(" india_selected");
								 india_selected = true;
								 break;
							}
						}	
					}
				}
				var currLst = document.reg_frm.setupCurr.value.split("^");

				if(india_selected){
					var currObj = document.reg_frm.transCurr;	
					currObj.length = currLst.length - 1;
					for(var a=1; a<currLst.length; a++){
						currObj.options[a-1] = new Option(currLst[a], currLst[a]);
					}	
				}else{
					var currObj = document.reg_frm.transCurr;
					var j = 0;
					currObj.length = currLst.length - 3;
					for(var a=1; a<currLst.length; a++){
						if(currLst[a] !='INR'){
							currObj.options[j] = new Option(currLst[a], currLst[a]);
							j++;
						}
					}	
				}
			}else{
				var currLst = document.reg_frm.setupCurr.value.split("^");
				if(india_selected){
					var currObj = document.reg_frm.transCurr;	
					currObj.length = currLst.length - 1;
					for(var a=1; a<currLst.length; a++){
						currObj.options[a-1] = new Option(currLst[a], currLst[a]);
					}	
				}else{
					var currObj = document.reg_frm.transCurr;
					var j = 0;
					currObj.length = currLst.length - 3;
					for(var a=1; a<currLst.length; a++){
						if(currLst[a] !='INR'){
							currObj.options[j] = new Option(currLst[a], currLst[a]);
							j++;
						}
					}	
				}
			}
			
			return true;
}

function thanks(){
	//document.reg_frm.actionPage.value = "/regn/regn_thankyou.jsp";
	document.reg_frm.actionPage.value ="/content/regn/registration4.jsp";
	document.reg_frm.submit();	
}

function rslDirect(){
	document.reg_frm.actionPage.value="/servlet/regn.servRslSubmit";
	document.reg_frm.submit();	
}

function moveNext(){
	document.reg_frm.process.value = "end";
	return reg_validate();
	
}

function doSubmit(){
	document.frmReseller.submit();	
}
