//date: ama feb.13, 2008
//
//aim: shipform.asp form specified ajax related jscript functions
//
//usage: include this script in your page
//
//<script src="shipform_jscript_ajax.js" type="text/javascript" language="javascript"></script>
//

var saved_country_code = '';
var saved_prov_state = '';
var dhl_products = new Array();
var flag_force_to_ajax = false;

function ajaxResponse(psLFuncName){
    var ajaxRespText = xmlHttp.responseText;
	flag_calling_ajax = false;
	data_exchange = '';
	debug_print("call ajaxResponse =["+ajaxRespText + ']');
	
    //debug
    switch(psLFuncName){
		case 'lf1':
            break;    
		case 'lf2':
            //populate options
            var svc_array = ajaxRespText.split('~');
            var f_ary;
            var part_num=0;
            //clean up array
            dhl_products = new Array();
            while (part_num < svc_array.length){
                f_ary = svc_array[part_num].split(';');
                dhl_products[dhl_products.length] = new Array(f_ary[0] ,f_ary[1] ,f_ary[2] ,f_ary[3] ,f_ary[4],f_ary[5] );
                part_num+=1;
            }
            populateOptionsServiceType();
            
		    flag_calling_ajax == false;
            
			var to_country_code = document.content.Sa_to_country.value;
			var to_prov_state = document.getElementById("Sa_to_prov_state").value;
			
			//alert('back from ajax:new='+to_country_code + ';old='+saved_country_code);

			var atmp = saved_ajax_params.split(';');
			
			//province drop-down            
			if (to_country_code != saved_country_code){
			    populateOptionsProvState(to_country_code,document.content.Sa_to_prov_state, atmp[2]);
				saved_country_code = to_country_code;
			}
			
			//document.getElementById("Sa_to_prov_state").value = atmp[2];
			
			if (to_country_code == 'CAN' || to_country_code == 'CA') {
				document.getElementById("Sa_to_prov_state").disabled = false;
				frm_show_ca_layout();
			} else if(to_country_code == 'USA' || to_country_code == 'US') {
				document.getElementById("Sa_to_prov_state").disabled = false;
				frm_show_intl_layout();
			} else {
				frm_show_intl_layout();
			}
            break;
		default:
		    alert('nothing happens');
		    break;
	} 
	//finished calling ajax
	flag_calling_ajax = false;
}

//prepare parameters for ajax call
var saved_ajax_params = '';

function ajaxRequest(psRFuncName, psRParameters, psLFuncName){
	var remote_func = '';
	var local_func = '';

	//alert(psRFuncName+ '??? ' + psLFuncName);
	
	debug_print('called function ajaxRequest('+ psRFuncName+','+psRParameters +','+psLFuncName +')');

	switch(psLFuncName){
		case 'lf1':
			remote_func = 'rf1';
			psRParameters = '';
			local_func = 'lf1';
            break;    
        //service list		  
		case 'lf2':
			var to_country = '';
			var to_prov_state = '';
			var flag_doc_nondoc = '';
			if (psRParameters == ''){
				to_country = document.content.Sa_to_country.value;
				to_prov_state = document.content.Sa_to_prov_state.value;
				flag_doc_nondoc = document.content.Sa_doc.checked;
			} else {
				var atmp = psRParameters.split(';');
				to_country = atmp[0];
				flag_doc_nondoc = atmp[1];
				to_prov_state = atmp[2];
				saved_prov_state = to_prov_state;
			}
		    
			//alert('flag='+flag_doc_nondoc);
			
		    if (to_country != 'CA' && to_country != 'US'){
				to_country = 'INTERNATIONAL';
				to_prov_state = '';
		    }
            
            if (saved_ajax_params == to_country+';'+flag_doc_nondoc+';'+to_prov_state){
				if (flag_force_to_ajax != true){
					return;
				}
            }else{
                saved_ajax_params = to_country+';'+flag_doc_nondoc+';'+to_prov_state;
            }
            
            //document.content.Sa_to_prov_state.disabled = true; 
            document.content.Sa_service_type.disabled = true; 
		    if (to_country == 'CA'){
				document.content.Sa_doc.disabled = true;
				document.content.Sa_nondoc.disabled = true;
				document.content.Sa_envelope_ind.disabled = false;
			} else {
				document.content.Sa_doc.disabled = false;
				document.content.Sa_nondoc.disabled = false;
				document.content.Sa_envelope_ind.disabled = true;
		    }
		            
            document.content.Sa_service_type.length = 0;
			document.content.Sa_service_type.options[0] = new Option('loading ...', '');

		    //parameters
		    remote_func = 'rf2';
			psRParameters = saved_ajax_params;
			local_func = 'lf2';
		  break;
		default:
		  alert('nothing happens');
		  break;
	} 
	//call ajax server portion
    flag_calling_ajax = true;
    debug_print('calling ajaxFunction(' + remote_func+','+psRParameters+','+local_func+')');
    //alert('calling ajaxFunction(' + remote_func+','+psRParameters+','+local_func+')');
    ajaxFunction(remote_func,psRParameters,local_func);
    flag_force_to_ajax = false;
}

function populateOptionsServiceType() {
	var selected = document.content.Sa_service_type.value;
	var sel_index = 0;
	var selbox = document.content.Sa_service_type;

	selbox.length = 0;
	for (n = 0; n < dhl_products.length; n++){ 
		selbox.options[n] = new Option(dhl_products[n][4], dhl_products[n] [0] + ';' + dhl_products[n] [1] + ';' + dhl_products[n] [2] + ';' + dhl_products[n] [3] + ';' + dhl_products[n] [5]);
	}
    document.content.Sa_service_type.disabled = false; 
}

//frm .. 
function frm_swap_doc_nonedoc(button) {
	if (button == 0 ){
		//check box non-doc (dutiable) clicked
		document.content.Sa_nondoc.checked = !document.content.Sa_doc.checked;
	} else {
		//check box doc (non-dutiable) clicked
		document.content.Sa_doc.checked = !document.content.Sa_nondoc.checked;
	} 
	//if both not checked, check non-doc for default
	if (!document.content.Sa_doc.checked && !document.content.Sa_nondoc.checked){
		document.content.Sa_nondoc.checked = true;
	}
	ajaxRequest('rf2','','lf2');
}

function frm_country_change() {
	var to_country_code = document.content.Sa_to_country.value;

//	debug_print ('saved_country_code='+saved_country_code + '; new to_country_code='+to_country_code);
	
//	if (to_country_code == saved_country_code){
//		return;
//	}

	debug_print ('called function frm_country_change()');
	
	if (to_country_code=='CA') {
		frm_show_ca_layout;

	} else if (to_country_code=='US'){
		frm_show_intl_layout;

	} else {
		frm_show_intl_layout;
	}
	ajaxRequest('rf2','','lf2');
}

function frm_prov_state_change() {
	var to_country_code = document.content.Sa_to_country.value;
	var to_prov_state_code = document.content.Sa_to_prov_state.value;

	if ((to_country_code == 'US') && (to_prov_state_code != saved_prov_state)){
		//alert('here prov change['+to_country_code+']['+to_prov_state_code+']['+saved_prov_state);
		ajaxRequest('rf2','','lf2');
	}else{
		return;
	}
}

function frm_show_ca_layout(){
}

function frm_show_intl_layout(){
}

function frm_service_type_change() {
	var to_country_code = document.getElementById("Sa_to_country").value;
	//US-Ground Only
	if (to_country_code != 'US'){
		return;
	}

	var Sa_service_type = document.getElementById("Sa_service_type").value;
	if (Sa_service_type == 'WD'){
		frm_show_ca_layout();
		showLayer('lyr2_usg');
		document.getElementById("Sa_service_type").disabled = true;
	} else {
		frm_show_intl_layout();
		document.getElementById("Sa_service_type").disabled = false;
	}
}

function frm_toggle_usg(){
	var to_country = document.getElementById("Sa_to_country").value;
	var Sa_service_type = document.getElementById("Sa_service_type").value;
	if (to_country != 'US'){
		return;
	}
	if (Sa_service_type == 'WD'){
		document.content.Sa_service_type.selectedIndex =0;
		document.getElementById("Sa_service_type").disabled = false;
		frm_show_intl_layout();
		document.getElementById("Sa_service_type").disabled = false;
	}
}

function frm_select_favorite_address(what) {
	var addr_str = what.favarite_addresses.value;
	
	if (addr_str == ''){
		what.Sa_to_address_id.value = '';
		what.Sa_to_person.value = '';
		what.Sa_to_company.value = '';
		what.Sa_to_adr_line1.value = '';
		what.Sa_to_adr_line2.value = '';
		what.Sa_to_city.value = '';
		what.Sa_to_prov_state.value = '';
		what.Sa_to_country.value = '';
		what.Sa_to_post_zip.value = '';
		what.Sa_to_tel_nbr.value = '';
		what.Sa_intl_to_iata.value = '';
		what.Sa_to_email.value = '';
		what.Sa_default_collect_account.value = '';
	} else {
	
		var addr_str_ary = addr_str.split("~");

		//alert(addr_str_ary[11]);
		//alert(addr_str);
		
		//alert(addr_str_ary[0]);
		//return;

		what.Sa_to_address_id.value = addr_str_ary[0];
		what.Sa_to_person.value = addr_str_ary[1];
		what.Sa_to_company.value = addr_str_ary[2];
		what.Sa_to_adr_line1.value = addr_str_ary[3];
		what.Sa_to_adr_line2.value = addr_str_ary[4];
		what.Sa_to_city.value = addr_str_ary[5];
		what.Sa_to_prov_state.value = addr_str_ary[6];
		what.Sa_to_country.value = addr_str_ary[7];
		what.Sa_to_post_zip.value = addr_str_ary[8];
		what.Sa_to_tel_nbr.value = addr_str_ary[9];
		if (addr_str_ary[10] == 'undefined') {
			what.Sa_intl_to_iata.value = '';
		} else {
			what.Sa_intl_to_iata.value = addr_str_ary[10];
		}
		if (addr_str_ary[11] == 'undefined') {
			what.Sa_to_email.value = '';
		} else {
			what.Sa_to_email.value = addr_str_ary[11];
		}
		if (addr_str_ary[12] == 'undefined') {
			what.Sa_default_collect_account.value = '';
		} else {
			what.Sa_default_collect_account.value = addr_str_ary[12];
			if (what.Sa_intl_chk_collect.checked == true){
				what.Sa_intl_collect_acct_nbr.value = addr_str_ary[12];
			}
			if (what.Sa_chk_collect.checked == true){
				what.Sa_collect_acct_nbr.value = addr_str_ary[12];
			}
		}
	}

	flag_force_to_ajax = true;
	ajaxRequest('rf2',addr_str_ary[7]+';;'+addr_str_ary[6],'lf2');
	
	what.Sa_to_prov_state.value = addr_str_ary[6];
}

function force_to_call_ajax(){
    debug_print('called function force_to_call_ajax()');
	flag_force_to_ajax = true;
	var data_exchange = document.content.data_exchange.value;
	//alert('data exchagne='+data_exchange);
	if (data_exchange != ''){
		debug_print('to call ajaxRequest from force_to_call_ajax with ' + data_exchange);
		ajaxRequest('rf2', data_exchange, 'lf2');
	} else {
		ajaxRequest('rf2','','lf2');
	}
}

function debug_print(psMsg){
	document.content.debuginfo.value = document.content.debuginfo.value + '\n' +  psMsg;
}

