/*
 * Copyright © Metafour UK Ltd
 * Filename  : register.js
 * Author    : Mahbubur Rahman Sumon
 * Created   : Dec 2, 2006
 * Functions : register a private client profile
 */
 // global variable
 var xTips = new XMLTips("Select address","");
 // store booking sources
 var Sources = new Array();
 
 // each source is a JS Object
function Source(code, desc, inls, exls){
	this.code = code;
	this.desc = desc;
	this.inls = inls;
	this.exls = exls;
}
 // submit a form
function doSubmit(frm)
{
        if (validateRegister(frm)) {
                frm.hidAction.value = "register";
                frm.submit();
        }
}

// validate register inputs
// 220108 added by Sunny: Validation for travel show competition entrance
function validateRegister(frm){
        var msgs = new Array(
                   "\nEnter a valid email address",
                   "\nEnter password",
                   "\nPlease re-enter correct password",
                   "\nEnter title",
                   "\nEnter first name",
                   "\nEnter surname name",
                   "\nEnter house name/flat number",
                   "\nEnter street address",
                   "\nEnter town/city/suburb",
                   "\nEnter postcode/zipcode",
                   "\nEnter state/province",
                   "\nEnter country",
                   "\nEnter at least one phone number with area code",
                   "\nSelect your source of information about us",
                   "\nEnter date of birth",
                   "\nSelect your answer to the competition question",
		   "\nTick the box to subscribe to email newsletters"
                   );
         var errmsg = "";
        if (frm.answer && !frm.answer[0].checked && !frm.answer[1].checked && !frm.answer[2].checked) {
                errmsg += msgs[15];
        }
        if ( !isEmail(frm.email.value.trim())){
                errmsg += msgs[0];
        }
        if ( frm.password && frm.password.value.trim() == "" && frm.is_pass_opt.value =='N'){
                errmsg += msgs[1];
        }
        if ( frm.is_pass_opt && frm.is_pass_opt.value =='N' && (frm.password2.value.trim() == "" || frm.password.value.trim() != frm.password2.value.trim())){
                errmsg += msgs[2];
        }
        if ( frm.title.value.trim() == ""){
                errmsg += msgs[3];
        }
        if ( frm.firstName.value.trim() == ""){
                errmsg += msgs[4];
        }
        if ( frm.lastName.value.trim() == ""){
                errmsg += msgs[5];
        }
	if ( frm.dob && frm.dob.value.trim() == "") {
		errmsg += msgs[14];
	}
        if ( frm.country.value.trim() == ""){
                errmsg += msgs[11];
        }
        if ( frm.postcode.value.trim() == ""){
                errmsg += msgs[9];
        }
        if ( frm.street.value.trim() == ""){
                errmsg += msgs[7];
        }
        if ( frm.city.value.trim() == ""){
                errmsg += msgs[10];
        }
        /* no one has area code + local number*/
         if ( (frm.wphone_area.value.trim() == "" ||  frm.wphone_local.value.trim() == "") &&
         	 (frm.hphone_area.value.trim() == "" ||  frm.hphone_local.value.trim() == "") && 
         	 (frm.mphone_area.value.trim() == "" ||  frm.mphone_local.value.trim() == "") ){
                errmsg += msgs[12];
        }
        if ( frm.source.value.trim() == ""){
                errmsg += msgs[13];
        }
	if ( frm.newsLetter && frm.newsLetter.type == "checkbox" && !frm.newsLetter.checked) {
		errmsg += msgs[16];
	}

        if (errmsg != "") {
                alert("Please correct the following errors to proceed:" + "\n" + errmsg);
                return false;
        }
        return true;
}
// Get client currency based on client's country
// it is directly copied from PHP and removed $ and declared currency variable
function get_client_currency(ctry_code){
    return ctry_code.substr(2,3);
}

// show currency note based on country selection
function showCurrencyNote(countryCode, spnId){
  	if (!countryCode || countryCode == ''){
  		showHide(spnId, false);
  		return;
  	}
  	var msg = "Your booking currency is determined by the country you are booking from which is ";
  	msg += get_client_currency(countryCode);
  	if (document.getElementById(spnId)){
		document.getElementById(spnId).innerHTML = msg;
	  	showHide(spnId, true);
 	}
}

/*
* Load booking source based on country selection
* 150108 added by Sunny: Select the client source if present, when updateing profile
*/
function loadBoSource(ct_ccd, src_code){
	if (!ct_ccd || ct_ccd == ''){
		return;
	}
	var ctryCode = ct_ccd.substr(0,2);
	var srcOptions = document.getElementById("source");
	var myRE = new RegExp(ctryCode);
  	var optIndx = 2;
  	// clear
    while (srcOptions.options.length > 2) {
        srcOptions.options[srcOptions.options.length-1] = null;
    }
  	for (var i=0; i<Sources.length; i++) {
		var src = Sources[i];
		var isApplicable = false;
		if (src.inls.length == 0 && src.exls.length == 0){
			isApplicable = true;
		}else{
			if (src.inls.length > 0 && src.inls.indexOf(ctryCode) >= 0){
				isApplicable = true;
			}else if (src.exls.length > 0 && src.exls.indexOf(ctryCode) == -1){
				isApplicable = true;
			}
		}
		if (isApplicable){
	    		srcOptions.options[optIndx] = new Option(src.desc, src.code);
			if (src_code != '' && src_code == src.code) {
				srcOptions.options[optIndx].selected = true;
                        }
	    		optIndx++;
	    	}
  	}
}
