<!--

var old_country ='';

function hf_get_obj (doc, name)
{
	if (doc.getElementById) return doc.getElementById(name);
	if (doc.all) return doc.all[name];
	if (doc.layers) return doc.layers[name];
	return false;
}

function hf_ajax_main ()
{
	http_request = false;
	if (window.XMLHttpRequest) 
	{ 
		// Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) 
  		http_request.overrideMimeType('text/xml');
	} 
	else 
	{
		if (window.ActiveXObject) 
		{ // IE
			try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} 
			catch (e) 
			{
				try {
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}
	}
	if (!http_request) 
	{
  	 alert('Cannot create XMLHTTP instance');
  	 return false;
	}
	return http_request;
}

function hf_getkey(e)
{
	if (window.event)
  	 return window.event.keyCode;
	else if (e)
  	 return e.which;
	else
  	 return null;
}

function change_id_content(idname,idvalue)
{
  var obj = hf_get_obj(document,idname);
  return change_obj_content(obj,idvalue);
}

function change_obj_content(obj,idvalue)
{
  if (!obj || !obj.style) 
  	return false;
  obj.innerHTML = idvalue;
  return true;
}

function hf_dummy_false()
{
	return false;
}

// Removes leading whitespaces
function hf_LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function hf_RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function hf_trim( value ) {
	
	return hf_LTrim(hf_RTrim(value));
	
}

function hf_limitText(e,fname, maxlimit)
{
	var key, keychar;
	key=hf_getkey(e);
	var obj=hf_get_obj(document,fname);
	if(!obj)
		return false;
	if(key==null)
	{
		if (obj.value.length > maxlimit) // if too long...trim it!
			obj.value = obj.value.substring(0, maxlimit);
		return true;	
	}

	// control keys
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
		return true;


	if (obj.value.length > maxlimit)
	{
		obj.value = obj.value.substring(0, maxlimit+1);
		return false;
	}	

	return true;
} 

function hf_select_payment_type(type,bank_id,cc_id)
{
	if(type=="")
	{
		hf_change_tr_display(bank_id,0);
		hf_change_tr_display(cc_id,0);
		return true;
	}
	if(type=='bank')
	{
		hf_change_tr_display(bank_id,1);
		hf_change_tr_display(cc_id,0);
		return true;
	}
	hf_change_tr_display(bank_id,0);
	hf_change_tr_display(cc_id,1);
	return true;
}

function hf_change_tr_display(idName,flag)
{
	// [2009.05.26 tkn]
	var trObj = document.getElementById(idName);
	
	if(!trObj)
		return false;
	if(flag)
		trObj.style.display='';
	else
		trObj.style.display='none';
}

function hf_payment_search_bank(knrId,blzId,sNameId,sLocId,notFoundError)
{
	var knrObj=hf_get_obj(document,knrId);
	var blzObj=hf_get_obj(document,blzId);
	if(!knrObj || !blzObj)
		return false;
	
	var knrValue=hf_trim(knrObj.value);
	var blzValue=hf_trim(blzObj.value);
	
	
	var sBNameValue='';
	var sBLocValue='';
	
	var url='';
	var xmlhttp=hf_ajax_main();

	if(!xmlhttp)
		return false;
	url =  GLOBAL_WEB_ROOT+'user_account/checkBank.php?knr='+escape(knrValue)+'&blz='+escape(blzValue);
	xmlhttp.open('GET', url);	
	xmlhttp.onreadystatechange = function() 
	{
		if (xmlhttp.readyState == 4 && (xmlhttp.status == 200 || xmlhttp.status == 205)) 
		{
			var responseContent = xmlhttp.responseText;		
			var mySplitResult = responseContent.split("\n");
			sBNameValue=notFoundError;
			sBLocValue='';

			var retCode=hf_trim(mySplitResult[0]);
			//alert(retCode);
			if(retCode=='1')
			{
				mySplitResult[2]=unescape(mySplitResult[2]);
				mySplitResult[3]=unescape(mySplitResult[3]);
				sBNameValue=hf_trim(mySplitResult[2]);
				sBLocValue=hf_trim(mySplitResult[3]);
			}
			change_id_content(sNameId,sBNameValue);
			change_id_content(sLocId,sBLocValue);
		}
		else
		{
			change_id_content(sNameId,sBNameValue);
			change_id_content(sLocId,sBLocValue);
			if(xmlhttp.readyState == 4 && xmlhttp.status == 404)
			{				
				content='Error processing data. URL not found!';
				alert(content);
			}
		}
	}
	xmlhttp.send(null);
}

function hx_vat_check_wait_message(msg)
{
	var url='';
	var xmlhttp=hf_ajax_main();
	var isFromEuropeanUnion;

	isFromEuropeanUnion = "false";

	if(!xmlhttp)
		return false;
		
	var country = hf_get_obj(document,'country');

	url =  GLOBAL_WEB_ROOT+'user_account/checkEuropeanUnion.php?country='+escape(country.value);
	xmlhttp.open('GET', url, false);

	xmlhttp.send(null);

	if (xmlhttp.status == 200)
		isFromEuropeanUnion = trim(xmlhttp.responseText);

	if (isFromEuropeanUnion == "true")
	{
		$.hiflex_processing({status : 'start'});

		var wait_obj=hf_get_obj(document,'vat_check_wait');
		if(!wait_obj || document.sform.country.value == '' || document.sform.tax_number.value == '') return false;	
	
		//wait_obj.innerHTML = msg;
	}
}

/*
function hx_vat_check_numeric_input(arr)
{	
	var key= hf_getkey(e);
	
	//alert(key);	
	if((key<48 || key>57) && key!=8 && key!=0)
	{		
		return false;		
	}
	
	return true;
}
*/

//-->
 

function SelectState(country)
{
	// [psz 07.10.2010] - bugfix: fixed showing and hiding of row with state select for USA and Canada.
	

	if (country == 'US' || country == 'CA')
	{
		$('#state_select_row').show();
		if ((country == 'US') && $('#county_select_row'))
			$('#county_select_row').show();
		else
			$('#county_select_row').hide();
	}
	else
	{
		$('#state_select_row').hide();
		if ($('#county_select_row'))
			$('#county_select_row').hide();
	}
	// [/psz]
	
	if(country == '' && document.getElementById('warper_states_NONE') )
		country = 'NONE';
		
	if(document.getElementById('warper_states_'+ old_country ) ) 
	{
		document.getElementById('states_'+ old_country ).disabled = 'disabled';
		document.getElementById('warper_states_'+ old_country ).style.display = 'none';	 
	}
	if(document.getElementById('warper_county_'+ old_country ) ) 
	{
		document.getElementById('county_'+ old_country ).disabled = 'disabled';
		document.getElementById('warper_county_'+ old_country ).style.display = 'none';	 
	}
	
	old_country = country;
	 
	if(document.getElementById('warper_states_'+ old_country ) )
	{
		document.getElementById('states_'+ old_country ).disabled = '';
		document.getElementById('warper_states_'+ old_country ).style.display = ''; 
	}
	if(document.getElementById('warper_county_'+ old_country ) )
	{
		document.getElementById('county_'+ old_country ).disabled = '';
		document.getElementById('warper_county_'+ old_country ).style.display = ''; 
	}
}


function checkStates()
{
	if(document.getElementById('country') )
	{
		var select = document.getElementById('country').value;
		if(select == '' && document.getElementById('warper_states_NONE') )
		{		
			SelectState('NONE');
			document.getElementById('warper_states_NONE').style.display='';
			
		}	
		else
			SelectState(select);
	}
}

// [MF] 20091209 - end of approval function

function hf_refresh_approval_nr(selId, nrId, fieldId) {
	var selObj, fieldObj, nrObj;
	selObj = hf_get_obj(document, selId);
	nrObj = hf_get_obj(document, nrId);
	fieldObj = hf_get_obj(document, fieldId);
	if (!selObj || !nrObj || !fieldObj) return false;

	var i, selectedCount, opLen, app_id_list, myOption;
	var freeSelected, noneSelected, freeValue, noneValue;
	var currentNrIndex, currentNrValue;
	opLen = selObj.options.length;
	freeSelected = noneSelected = false;
	noneValue = 'none';
	freeValue = 'free';
	app_id_list = '';
	selectedCount = 0;
	for (i = 0; i < opLen; i++) {
		if (selObj.options[i].selected) {
			selectedCount++;
			if (app_id_list.length == 0)
				app_id_list = selObj.options[i].value;
			else
				app_id_list += ';' + selObj.options[i].value;
			if (selObj.options[i].value == noneValue) {
				noneSelected = true;
			}
			else if (selObj.options[i].value == freeValue) {
				freeSelected = true;
			}
		}
	}
	if (noneSelected) {
		hf_select_remove_multiple_selection(selObj, noneValue);
		hf_select_remove_all_options(nrObj);
		myOption = new Option(0, 0);
		nrObj.options[0] = myOption;
		nrObj.options[0].selected = true;
		fieldObj.value = noneValue;
		selectedCount--;
	}
	else if (freeSelected) {
		hf_select_remove_multiple_selection(selObj, freeValue);
		hf_select_remove_all_options(nrObj);
		myOption = new Option(1, 1);
		nrObj.options[0] = myOption;
		nrObj.options[0].selected = true;
		fieldObj.value = freeValue;
		selectedCount--;
	}
	else {
		hf_select_remove_all_options(nrObj);
		if (selectedCount>0) {
			for (i=1; i<=selectedCount; i++) {
				myOption = new Option(i, i);
				nrObj.options[i-1] = myOption;
			}
			nrObj.options[selectedCount-1].selected = true;
		}
		else {
			myOption = new Option(0, 0);
			nrObj.options[0] = myOption;
			nrObj.options[0].selected = true;
		}
		fieldObj.value = app_id_list;
	}
}
// remove from a combo box all values selected except selectedValue
function hf_select_remove_multiple_selection(selObj, selectedValue) {
	var i, len = selObj.options.length;
	for (i = 1; i < len; i++) {
		if (selObj.options[i].value != selectedValue && selObj.options[i].selected) {
			selObj.options[i].selected = false;
		}
	}
}
function hf_select_remove_all_options(selObj) {
	if (!selObj) return false;
	var i, len = selObj.options.length;
	for (i = len-1; i >= 0; i--) {
		selObj.options[i] = null;
	}
}

// row_id_1 - approval_list row id
// row_id_2 - approval_nr row id
function hf_show_hide_approval(fname, row_id_1, row_id_2, nrId, fieldId) {
	nrObj = hf_get_obj(document, nrId);
	fieldObj = hf_get_obj(document, fieldId);
	var fObj = hf_get_obj(document, fname);
	var index, display_approval, usertype;
	if (!fObj) {
		return true;
	}
	if (fObj.selectedIndex<0) {
		// no usertype selected
		display_approval = false;
	}
	else {
		usertype = fObj.options[fObj.selectedIndex].value;
		usertype = usertype.toLowerCase();
		if (usertype == 'user') {
			// usertype == 'user'
			display_approval = true;
		}
		else {
			// for admin we don't display/set the approval
			display_approval = false;
		}
	}
	if (display_approval) {
		hf_change_tr_display(row_id_1, true);
		hf_change_tr_display(row_id_2, true);
	}
	else {
		hf_change_tr_display(row_id_1, false);
		hf_change_tr_display(row_id_2, false);
		// when hide - set nr of approval to zero and approval list to empty
		if (nrObj) {
			nrObj.value = '0';
		}
		if (fieldObj) {
			fieldObj.value = '';
		}
	}
	return true;
}

// [/MF] 20091209 - end of approval function

function disableCustomFields(arrEnable, arrDisable)
{
	for (var i in arrEnable)
	{
		if ($('#'+arrEnable[i]))
		{
			$('#'+arrEnable[i]).removeAttr("disabled");
			$('#'+arrEnable[i]).css('background-color', 'transparent');
		}
	}

	for (var i in arrDisable)
	{
		if ($('#'+arrDisable[i]))
		{
			$('#'+arrDisable[i]).attr("disabled", "disabled");
			$('#'+arrDisable[i]).css('background-color', '#D4D0C8');
		}
	}
}

