/*********************************************
* DESCRIPTION 
* Contact form validation routines
* UPDATE HISTORY
* 1.2 Rene Guzman,   08/21/2008, Added optional custom field.
* 1.3 Rene Guzman,   11/09/2009, Added validations for new Kunde styles.
**********************************************/
var localErrorMessage = "Error. Please correct the required fields.";


searchcountries = new Array (
[],
[cn_af],
[cn_al],
[cn_dz],
[cn_as],
[cn_ad],
[cn_ao],
[cn_ai],
[cn_ag],
[cn_ar],
[cn_am],
[cn_aw],
[cn_au],
[cn_at],
[cn_az],
[cn_bs],
[cn_bh],
[cn_bd],
[cn_bb],
[cn_ah],
[cn_by],
[cn_be],
[cn_bz],
[cn_bj],
[cn_bm],
[cn_bt],
[cn_bo],
[cn_an],
[cn_ba],
[cn_bw],
[cn_br],
[cn_vg],
[cn_bn],
[cn_bg],
[cn_bf],
[cn_bi],
[cn_kh],
[cn_cm],
[cn_ca],
[cn_cv],
[cn_ky],
[cn_cf],
[cn_td],
[cn_gb],
[cn_cl],
[cn_cn],
[cn_co],
[cn_cg],
[cn_cd],
[cn_ck],
[cn_cr],
[cn_hr],
[cn_an1],
[cn_cy],
[cn_cz],
[cn_dk],
[cn_dj],
[cn_dm],
[cn_do],
[cn_ec],
[cn_eg],
[cn_sv],
[cn_gq],
[cn_er],
[cn_ee],
[cn_et],
[cn_fo],
[cn_fj],
[cn_fi],
[cn_fr],
[cn_gf],
[cn_pf],
[cn_ga],
[cn_gm],
[cn_ge],
[cn_de],
[cn_gh],
[cn_gi],
[cn_gr],
[cn_gl],
[cn_gd],
[cn_gp],
[cn_gu],
[cn_gt],
[cn_gw],
[cn_gn],
[cn_gy],
[cn_ht],
[cn_hn],
[cn_hk],
[cn_hu],
[cn_is],
[cn_in],
[cn_id],
[cn_ir],
[cn_iq],
[cn_ie],
[cn_il],
[cn_it],
[cn_ci],
[cn_jm],
[cn_jp],
[cn_jo],
[cn_kz],
[cn_ke],
[cn_kw],
[cn_kg],
[cn_la],
[cn_lv],
[cn_lb],
[cn_ls],
[cn_lr],
[cn_ly],
[cn_li],
[cn_lt],
[cn_lu],
[cn_mo],
[cn_mk],
[cn_mg],
[cn_mw],
[cn_my],
[cn_mv],
[cn_ml],
[cn_mt],
[cn_mh],
[cn_mq],
[cn_mr],
[cn_mu],
[cn_mx],
[cn_fm],
[cn_md],
[cn_mc],
[cn_mn],
[cn_ms],
[cn_ma],
[cn_mz],
[cn_mm],
[cn_na],
[cn_np],
[cn_an2],
[cn_nl],
[cn_nc],
[cn_nz],
[cn_ni],
[cn_ne],
[cn_ng],
[cn_no],
[cn_om],
[cn_pk],
[cn_pw],
[cn_pa],
[cn_pg],
[cn_py],
[cn_pe],
[cn_ph],
[cn_pl],
[cn_pt],
[cn_pr],
[cn_qa],
[cn_re],
[cn_ro],
[cn_ru],
[cn_rw],
[cn_an3],
[cn_mp],
[cn_sm],
[cn_sa],
[cn_sn],
[cn_yu],
[cn_sc],
[cn_sl],
[cn_sg],
[cn_sk],
[cn_si],
[cn_so],
[cn_za],
[cn_kr],
[cn_es],
[cn_lk],
[cn_gp1],
[cn_vi],
[cn_an4],
[cn_kn],
[cn_lc],
[cn_an5],
[cn_vi1],
[cn_vc],
[cn_sd],
[cn_sr],
[cn_sz],
[cn_se],
[cn_ch],
[cn_sy],
[cn_tw],
[cn_tz],
[cn_th],
[cn_tg],
[cn_vg1],
[cn_tt],
[cn_tn],
[cn_tr],
[cn_tm],
[cn_tc],
[cn_vi2],
[cn_ug],
[cn_ua],
[cn_ae],
[cn_gb1],
[cn_us],
[cn_uy],
[cn_uz],
[cn_vu],
[cn_va],
[cn_ve],
[cn_vn],
[cn_wf],
[cn_ye],
[cn_zm],
[cn_zw]
);

function contactFieldCheck(vfld) {

	var vitem = vfld.name;
	var item = vfld.value;

	var itemInputField = document.getElementById(vitem);

	var re = /^\s*$/;      	// original regexp function test choked on textareas in safari, so here is a workaround
	if (item.match(re)) {  	// doing modified regexp test - on match, replacing string with blank space
		item = " ";       		// in next if statement, tests for the blank space.
	}

	if ((item=='') || (item==' ') || (item=='  ') || (item==null)) { 
		if(itemInputField!=null) itemInputField.className += " error";
		msg ("errmsg", localErrorMessage); 
		vfld.focus();
		return false;
	}else{
		if(itemInputField!=null) itemInputField.className = "txt";
	}
	return true;
}

function contactSelectFieldCheck(vfld) {

	var vitem = vfld.name;
	var item = vfld.value;

	var itemInputField = document.getElementById(vitem);

	var re = /^\s*$/;      	// original regexp function test choked on textareas in safari, so here is a workaround
	if (item.match(re)) {  	// doing modified regexp test - on match, replacing string with blank space
		item = " ";       		// in next if statement, tests for the blank space.
	}

	if ((item=='') || (item==' ') || (item=='  ') || (item==null)) { 
		if(itemInputField!=null) itemInputField.className += " error";
		msg ("errmsg", localErrorMessage); 
		vfld.focus();
		return false;
	}else{
		if(itemInputField!=null) itemInputField.className = "";
	}
	return true;
}

function emailFieldCheck(vfld) {
	var stat = contactFieldCheck(vfld); //checks if blank first
	if (stat != proceed) return stat;

	var vitem = vfld.name;
	var tfld = trim(vfld.value);  // value of field with whitespace trimmed off
	var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/
	if (!email.test(tfld)) {
		var itemInputField = document.getElementById(vitem);
		if(itemInputField!=null) itemInputField.className += " error";
		msg ("errmsg", localErrorMessage); 
		vfld.focus();
		return false;
	}
	return true;
};

function fnShowAllCtry( field, formName, firstOpt ){  
    var index = 0;
    var k = 0;
    var tmpVal;
    var totalElements = searchcountries.length;
    optElm  = document.forms[formName].elements[field];    
    k = firstOpt ? 1 : 0 ;

    for( j = k ; j < totalElements ; j++ ){
        tmpVal = searchcountries[j][0];        
        optElm.options[j] = new Option( tmpVal, tmpVal );      
   	}    
}

var lastSelectedCode = "blank";

function toggle(code) {
	if( document.getElementById && document.getElementById(code) != null ) {
		// toggle the lastSelectedCode to display = none
		document.getElementById(lastSelectedCode).style.display = 'none';

		// now, set the new selection to display = inline
		document.getElementById(code).style.display = 'inline';
		if(code == 'blank')
			document.getElementById(code).style.margin = '0 0 0 0';
		else
			document.getElementById(code).style.margin = '0 0 10px 0';

		// reset lastSelectedCode to the new code
		lastSelectedCode = code;
	}
}

function convertToUnicode(source) {
	result = ''; 
	for (i=0; i<source.length; i++) {		
		if( source.charCodeAt(i) =="13"  && source.charCodeAt(i + 1) =="10" ){
			result += '\n';
			i ++;
		}else{
			result += '&#' + source.charCodeAt(i) + ";";
		}
	}
	return result;
}

function validateOnSubmit() {
	document.forms.contactForm.hiddencuFirstName.value = convertToUnicode(document.forms.contactForm.elements['cuFirstName'].value);
	document.forms.contactForm.hiddencuLastName.value = convertToUnicode(document.forms.contactForm.elements['cuLastName'].value);
	document.forms.contactForm.hiddencuTitle.value = convertToUnicode(document.forms.contactForm.elements['cuTitle'].value);			
	document.forms.contactForm.hiddencuCompany.value = convertToUnicode(document.forms.contactForm.elements['cuCompany'].value);			

	if (document.forms.contactForm.elements['cuAddress'] != null){ 
		document.forms.contactForm.hiddencuAddress.value = convertToUnicode(document.forms.contactForm.elements['cuAddress'].value);			
	}
	document.forms.contactForm.hiddencuCity.value = convertToUnicode(document.forms.contactForm.elements['cuCity'].value);			
	document.forms.contactForm.hiddencuState.value = convertToUnicode(document.forms.contactForm.elements['cuState'].value);			
	document.forms.contactForm.hiddencuCountry.value = convertToUnicode(document.forms.contactForm.elements['cuCountry'].value);			
	document.forms.contactForm.hiddencuComments.value = convertToUnicode(document.forms.contactForm.elements['cuComments'].value);			

	// 1.2 begin
	if (document.forms.contactForm.hiddencuCustomField1 != null) {
		document.forms.contactForm.hiddencuCustomField1.value = convertToUnicode(document.forms.contactForm.elements['cuCustomField1'].value);			
	}
	// 1.2 end
	
	var errs=0;
	if (document.forms.contactForm.cuRecipient.value == 'blank') {
		var itemSelectField = document.getElementById(document.forms.contactForm.cuRecipient.name);
		if(itemSelectField!=null) itemSelectField.className = "error";
		errs += 1;
	}else{
		var itemSelectField = document.getElementById(document.forms.contactForm.cuRecipient.name);
		if(itemSelectField!=null) itemSelectField.className = "";
	}

	if (document.forms.contactForm.checkbox1) {
		if (!document.forms.contactForm.checkbox1.checked) {
		  msg ("errmsg", "Error. Please agree to the privacy agreement.");    
			return false;
		} else {
			document.forms.contactForm.hiddencheckbox1.value = convertToUnicode(document.forms.contactForm.elements['checkbox1'].value);			
		}
	}

	if (document.forms.contactForm.checkbox2) {
		if (document.forms.contactForm.checkbox2.checked) {
		  document.forms.contactForm.hiddencheckbox2.value = convertToUnicode(document.forms.contactForm.elements['checkbox2'].value);			 
		}
	}

	var elem;
	// execute all element validations in reverse order, so focus gets
	// set to the first one in error.
	if (!contactSelectFieldCheck(document.forms.contactForm.cuCountry)) errs += 1;
	if (!emailFieldCheck(document.forms.contactForm.cuEmail)) errs += 1;
	if (!contactFieldCheck(document.forms.contactForm.cuTel)) errs += 1;
	if (!contactFieldCheck(document.forms.contactForm.cuLastName)) errs += 1;
	if (!contactFieldCheck(document.forms.contactForm.cuFirstName)) errs += 1;

	return (errs==0);
  };
