//****************************************************************************************

function ChangeDivStyle(action,div_id,last_element){

	var current_div_style = document.getElementById(div_id).style; // take current div style
	var form_name = div_id.split('_'); // split - form name / div id
	var new_form_name,selectedID,set_form_name; 
	var i = 0;
	
	if(document.CreateArray != 1)
		{
			document.selected = new Array(); //create array at the first time you launch the wizard
			document.CreateArray = 1;
		}
	
	(form_name.length > 2)? new_form_name = form_name[0]+'_'+form_name[1] : new_form_name = form_name[0];

	(typeof(document.selected[new_form_name]) == 'undefined')? set_form_name = false: set_form_name = true;

	if(action == 'select') // only if the users select an item
		{
			for(i=0;i<=last_element;i++)
			{
					switch(parseInt(i)) // move selected style from all other elements in the group
					{
						case 0: 
							document.getElementById(new_form_name+'_'+i).style.backgroundImage = "url('"+document.ImagesPath+"list_first_item.gif')" ;
							document.getElementById(new_form_name+'_'+i).style.color = '#000000';
						break;
						case last_element: 
							document.getElementById(new_form_name+'_'+i).style.backgroundImage = "url('"+document.ImagesPath+"list_last_item.gif')" ;
							document.getElementById(new_form_name+'_'+i).style.color = '#000000';
						break;
						default: 
							document.getElementById(new_form_name+'_'+i).style.backgroundImage = "url('"+document.ImagesPath+"list_middel_item.gif')" ;
							document.getElementById(new_form_name+'_'+i).style.color = '#000000';
					}
			}
					switch(parseInt(form_name[1]))// change selected div image 
					{
						case 0: 
							current_div_style.backgroundImage = "url('"+document.ImagesPath+"list_first_item_selected.gif')" ;
						break;
						case last_element: 
							current_div_style.backgroundImage = "url('"+document.ImagesPath+"list_last_item_selected.gif')" ;
						break;
						default: 
							current_div_style.backgroundImage = "url('"+document.ImagesPath+"list_middel_item_selected.gif')" ;
					}
			current_div_style.color = '#ffffff' ; // change selected div text color
			document.selected[new_form_name] = div_id; // set selected variable to current one
		}
	
	if(action == 'out')
			{
				(set_form_name)?selectedID = document.selected[new_form_name].split('_'):'';
				if(document.selected[new_form_name] == div_id){ return true;} 
				else{ 
						for(i=0;i<=last_element;i++)
							{
								switch(parseInt(i))
								{
									case 0: 
										if(set_form_name){
										if(parseInt(i)==parseInt(selectedID[1]))
										{}else{document.getElementById(new_form_name+'_'+i).style.backgroundImage = "url('"+document.ImagesPath+"list_first_item.gif')"; 
												document.getElementById(new_form_name+'_'+i).style.color = '#000000';}
										}else{document.getElementById(new_form_name+'_'+i).style.backgroundImage = "url('"+document.ImagesPath+"list_first_item.gif')"; }
									break;
									case last_element: 
										if(set_form_name){
										if(parseInt(i)==parseInt(selectedID[1]))
										{}else{document.getElementById(new_form_name+'_'+i).style.backgroundImage = "url('"+document.ImagesPath+"list_last_item.gif')";
												document.getElementById(new_form_name+'_'+i).style.color = '#000000';}
										}else{document.getElementById(new_form_name+'_'+i).style.backgroundImage = "url('"+document.ImagesPath+"list_last_item.gif')";}
									break;	
									default: 
										if(set_form_name){
										if(parseInt(i)==parseInt(selectedID[1]))
										{}else{document.getElementById(new_form_name+'_'+i).style.backgroundImage = "url('"+document.ImagesPath+"list_middel_item.gif')";
												document.getElementById(new_form_name+'_'+i).style.color = '#000000';}
										}else{document.getElementById(new_form_name+'_'+i).style.backgroundImage = "url('"+document.ImagesPath+"list_middel_item.gif')";}
								}	
							}
					}
			}
	
	if(action == 'over')
			{	

				(set_form_name)?selectedID = document.selected[new_form_name].split('_'):'';
				if(document.selected[new_form_name] != div_id)
					{
						switch(parseInt(form_name[1]))
						{
							case 0:
								if(set_form_name){
								if(parseInt(i)==parseInt(selectedID[1]))
								{}else{current_div_style.backgroundImage = "url('"+document.ImagesPath+"list_first_item_over.gif')"; 
									   current_div_style.color = '#000000';}
								}else {current_div_style.backgroundImage = "url('"+document.ImagesPath+"list_first_item_over.gif')"; }
							break;
							case last_element:
								if(set_form_name){
								if(parseInt(i)==parseInt(selectedID[1]))
								{}else{current_div_style.backgroundImage = "url('"+document.ImagesPath+"list_last_item_over.gif')";
										current_div_style.color = '#000000';}
								}else{current_div_style.backgroundImage = "url('"+document.ImagesPath+"list_last_item_over.gif')";}
							break;
							default:
								if(set_form_name){
								if(parseInt(i)==parseInt(selectedID[1]))
								{}else{current_div_style.backgroundImage = "url('"+document.ImagesPath+"list_middel_item_over.gif')";
										current_div_style.color = '#000000';}
								}else{current_div_style.backgroundImage = "url('"+document.ImagesPath+"list_middel_item_over.gif')";}
						}
					}
			}
}

//****************************************************************************************

// AJAX 
/*
function SendValue(field_name,new_value,seed_id,show_result,show_loading){
	new_value = encodeURIComponent(new_value);
	var xmlHttp=null;
	try {xmlHttp=new XMLHttpRequest();}
	catch (e) { try {xmlHttp=new ActiveXObject('Msxml2.XMLHTTP');}
		catch (e) {	try {xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');}
			catch (e) { alert('Your browser does not support AJAX!'); return false;}
		}
	}
		var params = 'new_value='+new_value+'&field_name='+field_name+'&seed_id='+seed_id;
		var url = 'ajax_save_user_data.php';

		xmlHttp.open('POST', url, true);

		//Send the proper header information along with the request
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");

		xmlHttp.send(params);

		xmlHttp.onreadystatechange=function()
		{
			switch(xmlHttp.readyState)
			{
				case 4:
					if (xmlHttp.responseText=='true')
					{
						//if(show_result==true || show_result==1){document.getElementById(field_name+'_result').innerHTML=xmlHttp.responseText;}
						if(show_loading==true || show_loading==1){document.getElementById(field_name+'_loading').style.display = 'none';}
						pushToDataArray(field_name);
					}else{
						pullFromDataArray(field_name);
						alert(xmlHttp.responseText);
					}
				break; 
				default:
					if(show_result==true || show_result==1){document.getElementById(field_name+'_result').innerHTML='';}
					if(show_loading==true || show_loading==1){document.getElementById(field_name+'_loading').style.display = 'inline';}
			}
		}
}
*/


function SendValue(field_name,new_value,seed_id,show_result,show_loading){
	new_value = encodeURIComponent(new_value);

 $.ajax({
	   type: "POST",
	   url: "ajax_save_user_data.php",
	   data: 'new_value='+new_value+'&field_name='+field_name+'&seed_id='+seed_id,
	   async:false,
	   success: function(msg){
		   if (msg=='true')
				{
					//if(show_result==true || show_result==1){document.getElementById(field_name+'_result').innerHTML=xmlHttp.responseText;}
					if(show_loading==true || show_loading==1){document.getElementById(field_name+'_loading').style.display = 'none';}
					pushToDataArray(field_name);
				}else{
					pullFromDataArray(field_name);
					alert(xmlHttp.responseText);
				}
		},
		error: function(msg){
					pullFromDataArray(field_name);
					alert(xmlHttp.responseText);
		},
		beforeSend: function(){
					if(show_result==true || show_result==1){document.getElementById(field_name+'_result').innerHTML='';}
					if(show_loading==true || show_loading==1){document.getElementById(field_name+'_loading').style.display = 'inline';}
		}
 });
}
//****************************************************************************************

function AjaxValidation(validation_type,value,field_name,show_result,show_loading,save_value,seed_id,extra_param,direction){
	valueVal = encodeURIComponent(value);
	show_loading = (show_loading==1 || show_loading==true) ? true : false ;
	show_result = (show_result==1 || show_result==true) ? true : false ;
	var result,response,parts;

	$.ajax({
		type: "POST",
		async:false,
	    url: "ajax_validations.php",
	    data: 'value='+valueVal+'&extra_param='+extra_param+'&validation_type='+validation_type+'&direction='+direction,
	    success: function(msg){

			parts = msg.split('*|*|*');
			result = parts[1];
			response = parts[0];
			if(show_result){
				if (result=='true')
				{
					if (document.getElementById(field_name+'_result')){
						document.getElementById(field_name+'_result').innerHTML="";
					}
					if (document.getElementById(field_name+'_error')){
						document.getElementById(field_name+'_error').innerHTML=response;
					}
				}else if (result=='false'){
					if (document.getElementById(field_name+'_error')){
						document.getElementById(field_name+'_error').innerHTML="";
					}
					document.getElementById(field_name+'_result').innerHTML=response;
					var myTrash = response.split("_text'>");
					var rawResponse = myTrash[1].split("</div>");
					height_modify(field_name,rawResponse[0]);
				}
			}
			if(show_loading==true || show_loading==1){document.getElementById(field_name+'_loading').style.display = 'none';}
			if(save_value && result == 'true'){SendValue(field_name,value,seed_id,false,false);}else if (save_value && result=='false'){pullFromDataArray(field_name);}
		},
		beforeSend: function(){
			if(show_result==true || show_result==1){document.getElementById(field_name+'_result').innerHTML='';}
			if(show_loading==true || show_loading==1){document.getElementById(field_name+'_loading').style.display = 'inline';}
		},
		error: function(msg){
			pullFromDataArray(field_name);	
			alert(xmlHttp.responseText);
		}
 });
}


/*
function AjaxValidation(validation_type,value,field_name,show_result,show_loading,save_value,seed_id,extra_param,direction){

	valueVal = encodeURIComponent(value);
	show_loading = (show_loading==1 || show_loading==true) ? true : false ;
	show_result = (show_result==1 || show_result==true) ? true : false ;
	var xmlHttp=null,result,response,parts;
	try {xmlHttp=new XMLHttpRequest();}
	catch (e) { try {xmlHttp=new ActiveXObject('Msxml2.XMLHTTP');}
		catch (e) {	try {xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');}
			catch (e) { alert('Your browser does not support AJAX!'); return false;}
		}
	}
	var params = 'value='+valueVal+'&extra_param='+extra_param+'&validation_type='+validation_type+'&direction='+direction;
	var url = 'ajax_validations.php';

		xmlHttp.open('POST', url, true);
		//Send the proper header information along with the request
	
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");

		xmlHttp.send(params);

		xmlHttp.onreadystatechange=function()
		{
			switch(xmlHttp.readyState)
			{
				case 4:
					parts = xmlHttp.responseText.split('*|*|*');
					result = parts[1];
					response = parts[0];
					if(show_result){
						if (result=='true')
						{
							if (document.getElementById(field_name+'_result')){
								document.getElementById(field_name+'_result').innerHTML="";
							}
							if (document.getElementById(field_name+'_error')){
								document.getElementById(field_name+'_error').innerHTML=response;
							}
						}else if (result=='false'){
							if (document.getElementById(field_name+'_error')){
								document.getElementById(field_name+'_error').innerHTML="";
							}
							document.getElementById(field_name+'_result').innerHTML=response;
							var myTrash = response.split("_text'>");
							var rawResponse = myTrash[1].split("</div>");
							height_modify(field_name,rawResponse[0]);
						}
					}
					if(show_loading==true || show_loading==1){document.getElementById(field_name+'_loading').style.display = 'none';}
					if(save_value && result == 'true'){SendValue(field_name,value,seed_id,false,false);}else if (save_value && result=='false'){pullFromDataArray(field_name);}
				break; 
				default:
					if(show_result==true || show_result==1){document.getElementById(field_name+'_result').innerHTML='';}
					if(show_loading==true || show_loading==1){document.getElementById(field_name+'_loading').style.display = 'inline';}
			}
		}
}
*/
//****************************************************************************************

function height_modify(field_name,response){
	var contDiv;
	if (document.getElementById(field_name+'_container'))
	{
		contDiv = document.getElementById(field_name+'_container');

		if (response.length > 27 && contDiv.offsetHeight <50 && contDiv!=null)
		{
			contDiv.style.height = contDiv.offsetHeight + 24 + 'px';
		}
	}
}

//****************************************************************************************

function countChars(FieldName){
	var count = document.getElementById(FieldName).value;
	count = count.length;
	document.getElementById(FieldName+'_counter').innerHTML = count;
}

//****************************************************************************************

function clearTextArea(FieldName)
{
	if(document.CreateClearArray != 1)	{
		document.cleared = []; 
		document.CreateClearArray = 1;
	}
	if(document.cleared[FieldName] != 1){
		document.getElementById(FieldName).innerHTML = '';	
		document.cleared[FieldName] = 1;
	}
}

//****************************************************************************************

function startUpload(){
	document.getElementById('upload_loading').style.display = 'block';
	return true;
}

//****************************************************************************************

function stopUpload(success,msg,file_name){
    document.getElementById('upload_loading').style.display = 'none';
	if(success == 1){
		document.getElementById('cvoptions_buttons').style.display = 'none';
		document.getElementById('upload_section').style.display = 'none';
		document.getElementById('next_button').style.display = 'block';
		document.getElementById('upload_result_container').style.marginTop = '58px';
	}else{
		document.getElementById('upload_section').style.display = 'none';
	}
	document.getElementById('upload_result_container').style.display = 'block';
	document.getElementById('upload_result_label').innerHTML = msg;
	document.getElementById('upload_result_filename').innerHTML = file_name;
    return true;
}

//****************************************************************************************

function nextStep(last_div)
{
	if (document.MovingLock=='false')
	{
		document.MovingLock='true';
		setTimeout('advanceStep('+last_div+')',500);
	}else{
		return false;
	}
}
function advanceStep(last_div)
{
		var i,prog_bar_frame,prog_bar_width;

		//******************** Check mandatory fields - BEGIN
		document.getElementById('ErrorDiv').style.display = 'none';
		if(!checkMandatory(document.current_step) && document.ErrorFields.length > 0 )
		{
			var error_html = '';
			var direction=document.WizardDirection;
			var dir_right=(direction=='rtl')? 'right':'left';
			var dir_left=(direction=='rtl')? 'left':'right';
			
			for(x in document.ErrorFields)
			{
				error_html += '<div style="float:'+dir_right+';margin-'+dir_right+':20px;/*margin-top:7px;*/"><img src="'+document.ImagesPath+'cp_arrow.gif"></div><div class="error_list_text">'+document.ErrorFields[x]+'</div><div style="clear:both;"></div>';
			}
			displayError(error_html);
			return false
		}
		//******************** Check mandatory fields - END

		//******************** Analytics NextStep tracking - BEGIN
		if(typeof(pageTracker) == "object")
		{		
		pageTracker._trackPageview('/'+document.WizardName+'_'+document.current_step);
		}
		//******************** Analytics NextStep tracking - END

		if(document.current_step == last_div)
		{
			prog_bar_width = (parseInt(document.current_step))/last_div*100;
			startAt = parseInt(document.getElementById('progress_bar').style.width.split('%')[0]);
			ProgBar_final(startAt,prog_bar_width);
			return true;
		}

		opacity('wiz_box_'+document.current_step,100,0, 200);

		setTimeout(function(){showNextStep(last_div)},200);
}
function ProgBar_final(i, max){
	if (i<max)
	{
		document.getElementById('progress_bar').style.width = parseInt(i)+'%';
		setTimeout(function(){ProgBar_final(i+2,max)},10);
	}else{
		window.location = document.NextPageLink;
	}
}
function displayError(error_html){
	document.getElementById('ErrorDivContent').innerHTML = '<div style="height:7px;clear:both;"></div>'+error_html;
	document.getElementById('ErrorDiv').style.display = 'block';
	opacity('ErrorDiv',0,85, 300); 
	setTimeout(function(){opacity('ErrorDiv',85,0, 300);	setTimeout(function(){document.getElementById('ErrorDiv').style.display='none';document.MovingLock='false';},300);},3000);
}

//****************************************************************************************
function showNextStep(last_div){
	document.getElementById('wiz_box_'+document.current_step).style.display='none';
	document.current_step++;
	document.getElementById('wiz_box_'+document.current_step).style.display='block';
	setTimeout('opacity("wiz_box_'+document.current_step+'",0,100, 200);',50);
	setTimeout(function(){document.MovingLock='false';},50);
	prog_bar_width = (parseInt(document.current_step)-1)/last_div*100;
	startAt = parseInt(document.getElementById('progress_bar').style.width.split('%')[0]);
	ProgBar_advance(startAt,prog_bar_width);
	(document.current_step==1)? setStepStyle(1) : setStepStyle(2);
	return true;
}
function ProgBar_advance(i, max){
	if (i<max)
	{
		document.getElementById('progress_bar').style.width = parseInt(i)+'%';
		setTimeout(function(){ProgBar_advance(i+2,max)},10);
	}
}
//****************************************************************************************

function prevStep(last_div)
{
	if (document.MovingLock=='false')
	{
		document.MovingLock='true';
		var i,prog_bar_frame,prog_bar_width;
		
		if(document.current_step == 1)
		{
			//alert('this is the first div ... no way from here buddy');
			document.MovingLock='false';
			return false;
		}

		opacity('wiz_box_'+document.current_step,100,0, 200);

		setTimeout(function(){showPrevStep(last_div)},200);
	}else{
		return false;
	}
}

//****************************************************************************************
function showPrevStep(last_div){
	document.getElementById('wiz_box_'+document.current_step).style.display='none';
	document.current_step--;
	document.getElementById('wiz_box_'+document.current_step).style.display='block';
	setTimeout('opacity("wiz_box_'+document.current_step+'",0,100, 200);',50);
	setTimeout(function(){document.MovingLock='false';},50);

	prog_bar_width = (parseInt(document.current_step)-1)/last_div*100;
	startAt = parseInt(document.getElementById('progress_bar').style.width.split('%')[0]);
	ProgBar_retreat(startAt,prog_bar_width);
	(document.current_step==1)? setStepStyle(1) : setStepStyle(2);
	return true ;
}
function ProgBar_retreat(i, max){
	if (i>max)
	{
		document.getElementById('progress_bar').style.width = parseInt(i)+'%';
		setTimeout(function(){ProgBar_retreat(i-2,max)},10);
	}
}
//****************************************************************************************

function opacity(id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    }
}

//****************************************************************************************

function changeOpac(opacity, id) {
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")"; 
}

//****************************************************************************************

function setStepStyle(step)
{

	if(step == 1)
	{
		document.getElementById("prevDiv").style.cursor = 'default';
		document.getElementById('prev_button_middle').style.backgroundImage = "url('"+document.ImagesPath+"prevbutton_middle_da.gif')"; 
		document.getElementById('prev_button_right').src = document.ImagesPath+'prevbutton_right_da.gif'; 
		document.getElementById('prev_button_left').src = document.ImagesPath+'prevbutton_left_da.gif'; 
	}else{
		document.getElementById("prevDiv").style.cursor = 'pointer';
		document.getElementById('prev_button_middle').style.backgroundImage = "url('"+document.ImagesPath+"prevbutton_middle.gif')"; 
		document.getElementById('prev_button_right').src = document.ImagesPath+'prevbutton_right.gif'; 
		document.getElementById('prev_button_left').src = document.ImagesPath+'prevbutton_left.gif'; 
	}
}

//****************************************************************************************

function setCurrentstep() {
	document.current_step = 1;
	if (document.getElementById("load"))
	{
		document.getElementById("load").style.display = 'none';
		setStepStyle(1);
	}
	if(typeof(document.Saved)==='object'){UpdateSavedDataFromCookie();}

	return true;
}

function UpdateSavedDataFromCookie(){
	var SavedArr = getCookieValue('Saved');
	var arr = [];
	var CookieValue='';
	var x;
	arr = String(SavedArr).split('*#');
	for(x in arr){
		document.Saved[x]=parseInt(arr[x]);
	}
}

//****************************************************************************************
window.onload = setCurrentstep;
//****************************************************************************************

function checkMandatory(group) {
	var i;
	var check = true;
	document.ErrorFields=[];
		for(i=0;i<=document.GroupID.length;i++)
		{
			if(document.GroupID[i] == group){
				if(document.Mandatory[i]== 1){
					if(document.Saved[i]==0)
						{
							check=false;
							document.ErrorFields.push(document.ErrorText[i]);
						}
				}
			}
		}
		if(check)
		{
			return true;
		}else{
			return false;
		}
}

//****************************************************************************************

function pullFromDataArray(field_name) {
	var i;
		for(i in document.ComponentName)
		{
			if(document.ComponentName[i]==field_name)	
			{	document.Saved[i]=0; UpdateSavedDataCookie(0,i);	break;	}
		}
	return true;
}


//****************************************************************************************

function pushToDataArray(field_name) {
	var i,pos,group;
	var savedCount=0;
	var numberOfComp=0;
	var numberOfGroups=0;
		
	for(i=0;i<=document.ComponentName.length;i++)
	{
		if(document.ComponentName[i] == field_name)
			{
				pos=i;
				break;
			}
	}
	document.Saved[pos]=1;
	UpdateSavedDataCookie(1,pos);
	group = document.GroupID[pos];

	for(i=0;i<=document.GroupID.length;i++)
	{
		if(document.GroupID[i] != document.GroupID[i+1]){numberOfGroups++ ;}
		if(document.GroupID[i] == group)
		{	
			numberOfComp++;
			if (document.Saved[i]==1)
			{
				savedCount++;
			}
		}
	}
	if ((numberOfComp==savedCount) && (document.AutoProg[pos]==1) && (checkMandatory(group))) {nextStep(numberOfGroups);}

	return true;
}

function UpdateSavedDataCookie(value,pos){
	var SavedArr = getCookieValue('Saved');
	var arr = [];
	var CookieValue='';
	arr = String(SavedArr).split('*#');
	arr[pos]=value;
	CookieValue=arr.join('*#');
	
	setcookie('Saved',CookieValue,2);
}

//****************************************************************************************
//															COMBOS and COUNTRY

function OpenCityCombo(field_name, countryCode, regionCode, seed_id, region_sent, direction){
	var region_sent = (region_sent == null)? false : region_sent;
	var xmlHttp,result,response,parts;
	try {xmlHttp=new XMLHttpRequest();}
	catch (e) { try {xmlHttp=new ActiveXObject('Msxml2.XMLHTTP');}
		catch (e) {	try {xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');}
			catch (e) { alert('Your browser does not support AJAX!'); return false;}
		}
	}
	var params = 'field_name='+field_name+'&country_code='+countryCode+'&region_code='+regionCode+'&seed_id='+seed_id+'&direction='+direction+'&region_sent='+region_sent;
	var url = 'ajax_city_list.php';
	xmlHttp.open('POST',url,true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.onreadystatechange=function()
		{
			switch(xmlHttp.readyState)
			{
				case 4:
					CityDiv = document.getElementById('_city');
					var myResponse = xmlHttp.responseText;
					var myTrash = myResponse.split("<script type='text/javascript' language='javascript'>");
					var cityScript = myTrash[1].split("</script>");
					eval(cityScript[0]);
					CityDiv.innerHTML = xmlHttp.responseText;
					if (document.lastAjaxElementLoaded)
					{
							//**************** this is the place to set the focus of the first form element
							//document.getElementById(field_name+'_text').focus();
							document.lastAjaxElementLoaded=false;
					}
					if (region_sent)
					{
						setTimeout(function() {document.getElementById('City_text').focus();}, 50);
					}
				break; 
				default:
			}
		}
		setTimeout(function() {document.getElementById('Region_result').innerHTML='';},50);
	xmlHttp.send(params);
}

//****************************************************************************************

function OpenRegionCombo(field_name, code, seed_id, country_sent, direction){
	var country_sent = (country_sent == null)? false : country_sent;
	var xmlHttp,result,response,parts;
	try {xmlHttp=new XMLHttpRequest();}
	catch (e) { try {xmlHttp=new ActiveXObject('Msxml2.XMLHTTP');}
		catch (e) {	try {xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');}
			catch (e) { alert('Your browser does not support AJAX!'); return false;}
		}
	}
	var params = 'field_name='+field_name+'&code='+code+'&seed_id='+seed_id+'&direction='+direction+'&country_sent='+country_sent;
	var url = 'ajax_region_list.php';
	xmlHttp.open('POST',url,true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.onreadystatechange=function()
		{
			switch(xmlHttp.readyState)
			{
				case 4:
					RegionDiv = document.getElementById('_region');
					var myResponse = xmlHttp.responseText;
					var myTrash = myResponse.split("<script type='text/javascript' language='javascript'>");
					var regionScript = myTrash[1].split("</script>");
					eval(regionScript[0]);
					RegionDiv.innerHTML = xmlHttp.responseText;
					if (country_sent)
					{
						setTimeout(function() {document.getElementById('Region_text').focus();}, 50);
					}
				break; 
				default:
			}
		}
		setTimeout(function() {document.getElementById(field_name+'_result').innerHTML='';},50);
	xmlHttp.send(params);
}

//****************************************************************************************

function selectItem(new_value, field_name, send){
	var myValue = window.document.getElementById(field_name+'_value');
	var myText = window.document.getElementById(field_name+'_text');
	myValue.value=new_value;
	myText.value=new_value;
	if (send==true){
		showHideScroll(field_name);
	}
}

//****************************************************************************************

function showHideScroll(field_name){
	var listArray = eval("document."+field_name+"_listArray");
	var matches = eval("document."+field_name+"_matches");
	var myList = window.document.getElementById(field_name);

	if (myList.style.display=='none')
	{
			var myText = window.document.getElementById(field_name+"_text");
			myText.focus();
			myList.style.display='';
			for (x in listArray)
			{
				showItem(field_name+'_'+x);
				matches[x]=1;
			}
			var defaultHeight = window.document.getElementById(field_name+'_scrollHeight').value;
			var wholeList = window.document.getElementById(field_name);
			listHeight = defaultHeight;
			wholeList.style.height = listHeight + 'px';
			//********** hide all other scrolls on page
			for (x in document.scrolldowns)
			{
				if (document.scrolldowns[x]!=field_name)
				{
					hideScroll(document.scrolldowns[x]);
				}
			}
	}else{
			myList.style.display='none';
			for (x in listArray)
			{
				hideItem(field_name+'_'+x);
				matches[x]=0;
			}
	}
}

//****************************************************************************************

function hideScroll(field_name){
	var listArray = eval("document."+field_name+"_listArray");
	var matches = eval("document."+field_name+"_matches");
	var myList = window.document.getElementById(field_name);
			myList.style.display='none';
			for (x in listArray)
			{
				hideItem(field_name+'_'+x);
				matches[x]=0;
			}
}

//****************************************************************************************

function highlightItem(itemID){
	var myItem = window.document.getElementById(itemID);
	myItem.style.background='#DBE7F5'; }
function lowlightItem(itemID){
	var myItem = window.document.getElementById(itemID);
	myItem.style.background='#ffffff'; }
function showItem(itemID){
	var myItem = window.document.getElementById(itemID);
	myItem.style.display=''; }
function hideItem(itemID){
	var myItem = window.document.getElementById(itemID);
	myItem.style.display='none'; }
function seekAndShow(varText, field_name, e){
	var keyPressed = (e.which)? e.which: event.keyCode;
	if (keyPressed==40 || keyPressed==38 || keyPressed==13 || keyPressed==9) { return false; }
	var listArray = eval("document."+field_name+"_listArray");
	var matches = eval("document."+field_name+"_matches");
	var selection = eval("document."+field_name+"_selection");
	var matchFound = false;
	for (x in listArray)
		{
			arrValue = listArray[x];
			arrValue = arrValue.toLowerCase();
			if (varText != '')
			{
				varText = varText.toLowerCase();
				if  (arrValue.indexOf(varText) == 0)
				{
					for (a in selection){selection[a]=0;}
					matches[x]=1;
					//*********** open scrolldown if closed
					if (document.getElementById(field_name).style.display=='none'){document.getElementById(field_name).style.display='';}
					//*********** show found search item
					showItem(field_name+'_'+x);
					matchFound = true;
				}else{
					matches[x]=0;
				}
			}else{
				//*********** feed search matches array with NO RESULT
				for (a in matches) {matches[a]=0;}
				for (a in selection) {selection[a]=0;}
				hideItem(field_name+'_'+x);
				//*********** close scrolldown if open
				if (document.getElementById(field_name).style.display==''){document.getElementById(field_name).style.display='none';}
			}
		}
	var defaultHeight = window.document.getElementById(field_name+'_scrollHeight').value;
	var wholeList = window.document.getElementById(field_name);
	var listHeight = wholeList.style.height;
	listHeight = listHeight.substr(0,listHeight.indexOf('px'));
		wholeItem = window.document.getElementById(field_name+'_text');
		itemHeight = wholeItem.offsetHeight + 1;


	//*********** hide all options not matching search criteria
	var matchesHeight = 0;
	for (a in listArray){
		if (matches[a]==0){
			hideItem(field_name+'_'+a);
		}else if (matches[a]==1) {
			matchesHeight += parseInt(itemHeight);
		}
	}
	//*********** change size of list to matchesHeight if its smaller than defaultHeight
	if (matchesHeight < defaultHeight)
	{
		listHeight = matchesHeight;
		wholeList.style.height = listHeight + 'px';
	}else{
		listHeight = defaultHeight;
		wholeList.style.height = listHeight + 'px';
	}

	//*********** hide entire menu if no match was found
	if (matchFound == false)
	{
		if (document.getElementById(field_name).style.display==''){document.getElementById(field_name).style.display='none';}
		for (a in matches)	{matches[a]=0;}	for (a in selection){selection[a]=0;}
	}
}

//****************************************************************************************

function CheckValueCountry(field_name, field_value, seed_id, show_result, show_loading, notInListMessage, display_region, display_city, country_code, direction, parentField){
	if (field_value==''){return false;}
	//* trim
	field_value = field_value.replace(/^\s+|\s+$/g,'');
	field_value = field_value.toLowerCase();

	var display_region = (display_region == null)? false : display_region;
	var display_city = (display_city == null)? false : display_city;

	fieldOk = false;
	var listArray = eval("document."+field_name+"_listArray");
	for (a in listArray)
	{
		current_item = listArray[a];
		current_item = current_item.replace(/^\s+|\s$/g,'');
		current_item = current_item.toLowerCase();
		if (field_value == current_item)
		{
			fieldOk = true;
			fieldTrueValue = listArray[a];
			if (field_name=='Country' || field_name=='Region')
			{
				fieldCode = window.document.getElementById(field_name+'_'+a+'_code').value;
				document.getElementById(field_name+'_value').value=fieldCode;
			}

			if (display_region==true)
			{
				OpenRegionCombo(field_name, fieldCode, seed_id, true, direction);
			}else if (display_city==true){
				OpenCityCombo(parentField, country_code, fieldCode, seed_id, true, direction);
			}

			if (field_name=='City')
			{
				SendValue('Country',document.getElementById('Country_value').value,seed_id,show_result,show_loading);
				SendValue('Region',document.getElementById('Region_value').value,seed_id,show_result,show_loading);
				SendValue(field_name,fieldTrueValue,seed_id,show_result,show_loading);
			}
			window.document.getElementById(field_name+'_result').innerHTML = "";
			return true;
		}
	}
	if (fieldOk == false)
	{
		errWidth = window.document.getElementById(field_name+'_result').style.width;
		errFloat = (document.all) ? window.document.getElementById(field_name).style.styleFloat : window.document.getElementById(field_name).style.cssFloat;
		myHtml = "<div class='ajax_validation_error' style='float: "+errFloat+"; text-align: "+errFloat+"; background-position: "+errFloat+" center;'></div>";
		myHtml += "<div class='ajax_validation_error_text' style='float: "+errFloat+";'>"+notInListMessage+"</div>";
		window.document.getElementById(field_name+'_result').innerHTML = myHtml.replace(/\n/,'');
		return false;
	}
}

//****************************************************************************************

function CheckValue(field_name, field_value, seed_id, show_result, show_loading, notInListMessage, display_region, display_city, country_code, direction, parentField){
	if (field_value==''){return false;}
	//* trim
	field_value = field_value.replace(/^\s+|\s+$/g,'');
	field_value = field_value.toLowerCase();

	var display_region = (display_region == null)? false : display_region;
	var display_city = (display_city == null)? false : display_city;

	fieldOk = false;
	var listArray = eval("document."+field_name+"_listArray");
	for (a in listArray)
	{
		current_item = listArray[a];
		current_item = current_item.replace(/^\s+|\s$/g,'');
		current_item = current_item.toLowerCase();
		if (field_value == current_item)
		{
			fieldOk = true;
			fieldTrueValue = listArray[a];
			SendValue(field_name,fieldTrueValue,seed_id,show_result,show_loading);
			window.document.getElementById(field_name+'_result').innerHTML = "";
			return true;
		}
	}
	if (fieldOk == false)
	{
		errWidth = window.document.getElementById(field_name+'_result').style.width;
		errFloat = (document.all) ? window.document.getElementById(field_name).style.styleFloat : window.document.getElementById(field_name).style.cssFloat;
		myHtml = "<div class='ajax_validation_error' style='float: "+errFloat+"; text-align: "+errFloat+"; background-position: "+errFloat+" center;'></div>";
		myHtml += "<div class='ajax_validation_error_text' style='float: "+errFloat+";'>"+notInListMessage+"</div>";
		window.document.getElementById(field_name+'_result').innerHTML = myHtml.replace(/\n/,'');
		return false;
	}
}

//****************************************************************************************

function listKeyPressed(field_name, e, notInListMessage){
	var listArray = eval("document."+field_name+"_listArray");
	var matches = eval("document."+field_name+"_matches");
	var selection = eval("document."+field_name+"_selection");
	var keyPressed = (e.which)? e.which: event.keyCode;
	if (keyPressed!=40 && keyPressed!=38 && keyPressed!=13) { return false; }
	if (keyPressed==40)
	{
		if (document.getElementById(field_name).style.display=='none')
		{
			showHideScroll(field_name);
			return false;
		}
		for (a in matches)
		{
			if (matches[a]==1)
			{
				//********************** check if something is selected at all
				isSomethingSelected = false;
				for (y in selection){if (selection[y]==1){isSomethingSelected = true;}}
				if (isSomethingSelected)
				{
						if (selection[a]==1)
						{
							//************************ handle selection move DOWN
							for (x=parseInt(a)+1; x<listArray.length; x++)
							{
								if (matches[x]==1)
								{
									for (q in selection){if (q!=x){selection[q]=0;}}
									for (q in matches)	{if (q!=x){lowlightItem(field_name+"_"+q) ;}}
									selection[x]=1;
									highlightItem(field_name+"_"+x);
									selectItem(listArray[x],field_name,false);
									return true;
								}
							}
						}
				}else{
						for (q in selection){if (q!=a){selection[q]=0;}}
						for (q in matches)	{if (q!=a){lowlightItem(field_name+"_"+q) ;}}
						selection[a]=1;
						highlightItem(field_name+"_"+a);
						selectItem(listArray[a],field_name,false);
						return true;
				}
			}
		}
	}else if(keyPressed==38){
		for (a in matches)
		{
			if (matches[a]==1)
			{
				//********************** check if something is selected at all
				isSomethingSelected = false;
				for (y in selection) {if (selection[y]==1){isSomethingSelected = true;}}
				if (isSomethingSelected)
				{
						if (selection[a]==1)
						{
							//************************ handle selection move UP
							for (x=parseInt(a)-1; x>-1; x--)
							{
								if (matches[x]==1)
								{
									for (q in selection) {if (q!=x){selection[q]=0;}}
									for (q in matches)	{if (q!=x){lowlightItem(field_name+"_"+q) ;}}
									selection[x]=1;
									highlightItem(field_name+"_"+x);
									selectItem(listArray[x],field_name,false);
									for (q in matches)	{if (q!=x){lowlightItem(field_name+"_"+q) ;}}
									return true;
								}
							}
						}
				}else{
						for (q in selection) {if (q!=a){selection[q]=0;} }
						for (q in matches) {if (q!=a){lowlightItem(field_name+"_"+q) ;} }
						selection[a]=1;
						highlightItem(field_name+"_"+a);
						selectItem(listArray[x],field_name,false);
						return true;
				}
			}
		}
	}else if(keyPressed==13){
		fieldOk = false;
		field_value = window.document.getElementById(field_name+'_text').value;
		for (s in listArray)
		{
			test_value = field_value.replace(/^\s+|\s+$/g,'');
			test_value = test_value.toLowerCase();
			list_value = listArray[s].toLowerCase();
			list_value = list_value.replace(/^\s+|\s+$/g,'');
			if (list_value == test_value){
				fieldOk = true;
				field_value = listArray[s];
				continue;
			}
		}
		if (fieldOk==true)
		{
			selectItem(field_value,field_name,true); 
		}else{
			errWidth = window.document.getElementById(field_name+'_result').style.width;
			errFloat = (document.all) ? window.document.getElementById(field_name).style.styleFloat : window.document.getElementById(field_name).style.cssFloat;
			myHtml = "<div class='ajax_validation_error' style='float: "+errFloat+"; text-align: "+errFloat+"; background-position: "+errFloat+" center;'></div>";
			myHtml += "<div class='ajax_validation_error_text' style='float: "+errFloat+";'>"+notInListMessage+"</div>";
			window.document.getElementById(field_name+'_result').innerHTML = myHtml.replace(/\n/,'');
		}
	}
}

