var site = '';
var tld = '';

function xmlhttpPost(strURL,step,target) 
{
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText,target);
        }
    }
    self.xmlHttpReq.send(getquerystring(step));
}

function getquerystring(step) 
{
	switch (step){
		case 'country':
			var country = document.getElementById('country');
			
			var qstr = 'country=' + escape(country.value);  // NOTE: no '?' before querystring
			qstr = qstr + '&step=country';
		break;
		case 'state':
			var country = document.getElementById('country');
			var state = document.getElementById('state');
			
			var qstr = 'country=' + escape(country.value);  // NOTE: no '?' before querystring
			qstr = qstr + '&state=' + escape(state.value);
			qstr = qstr + '&step=state';
		break;
		default:
		break;
	}
	return qstr;
}

// Change object from AJAX
function updatepage(result, target){
	// alert(result);
	if (result != 'error'){
		var t=target + '0';
		if(target=='country'){
			var new_target = 'state';
		} else {
			var new_target = 'city';
		}
		if(target == 'city'){
			var select_field = "<select id='city' name='city' class='B2BSmallFontfield' style='WIDTH: 165px'>";
			var show_res = select_field + getData(result) + '</select>';
		} else {
			var select_field = "<select id='"+target+"' name='"+target+"' class='B2BSmallFontfield' style='WIDTH: 165px' onchange='if(0!=this.value){loading(); xmlhttpPost(\"/?a=new_landing_ajax\",\""+target+"\",\""+new_target+"\");}' onclick='if(document.getElementById(\"state\").value == 0 || (document.getElementById(\"state\").value !=0 && document.getElementById(\"country\").value == 0)){document.getElementById(\"city\").disabled=\"true\"; document.getElementById(\"submit_button\").disabled=\"true\";} '>";
			var show_res = select_field + getData(result) + '</select>';
		}
		// alert(show_res);
    	document.getElementById(t).innerHTML = show_res; //s.split("|")[1];
    	
    	if(target == 'city'){
			document.getElementById("city").disabled=false;
			document.getElementById('loading').innerHTML = '';
			document.getElementById("submit_button").disabled= false;
			setTimeout("doSelectUserCity()",300);
		} else {
		    
		  
			document.getElementById("city").disabled = true;
			document.getElementById('loading').innerHTML = '';
			
			
			
			if(document.getElementById("state").value == 0 || (document.getElementById("state").value != 0 && document.getElementById("country").value == 0))
			{
			  
			    document.getElementById("submit_button").disabled = true;
			}
			else
			{
			     document.getElementById("submit_button").disabled = false;
			}
			
			 
			setTimeout("doSelectUserState()",300); 
		}
    } else {
		document.getElementById(target).innerHTML = 'Thank you! Your friend will be informed about this site.';
	}
}

function getData(result)
{
	var data = result.split("|");
	var j = 0; var ret = '';
	for(var i=0;i<(data.length-1);i++)
	{
		j = i + 1;
		ret = ret + '<option value="' + data[i] + '">' + data[j] + '</option>';
		i++;
	}
	// alert(ret);
	return ret;
}

// If loading data from PHP then show Loading element
function loading()
{
	document.getElementById('loading').innerHTML = '&nbsp;<img src="../img/spinner.gif">';
}

function enterCityName(country)
{
	document.getElementById('changableFields').innerHTML = "<table width='100%' celpadding='0' cellspacing='0'><tr><td width='130'><span class='B2BStandardFontBoldBlack'>City:</span><span class='B2BAsterick'>*</span></td><td><span class='form_value' id='city0'><input type='text' name='city' id='city' AUTOCOMPLETE='off' onkeyup='checkCities(this.value,\"" + country + "\");'><br /><div id='city_suggestions' style='position: absolute; display: none; width: 142px; background: #FFFFFF; border: solid 1px #000000;'></div></span></td></tr></table>";
	document.getElementById("submit_button").disabled= false;
}

function stateIsUSorCA()
{
	document.getElementById('changableFields').innerHTML = "<table width='100%' celpadding='0' cellspacing='0'><tr>				    	<td width='130'><span class='B2BStandardFontBoldBlack'>State/Province:</span><span class='B2BAsterick'>*</span></td>				    	<td><span class='form_value' id='state0'>				    	<select id='state' name='state' class='B2BSmallFontfield' style='WIDTH: 165px' onchange='if(0!=this.value){xmlhttpPost(\"/?a=new_landing_ajax\",\"state\",\"city\");}'>				    	<option value=''>Choose a state</option>				    	</select>				    </span>				    </td>				  </tr>				  <tr>				    <td width='130'><span class='B2BStandardFontBoldBlack'>City:</span><span class='B2BAsterick'>*</span></td>				    <td>				    <span class='form_value' id='city0'>				    	<select id='city' name='city' class='B2BSmallFontfield' style='WIDTH: 165px';>				    		<option value=''>Choose a city</option>				    	</select>				    </span>				    <span id='loading' style='font-family: Verdana; font-size: 10px; color: red;'></span>				    </td>				  </tr></table>";
}

function checkCities(city, country)
{
	document.getElementById('city_suggestions').style.display = 'block';
	getCitiesFromPHP(city, country);
//	document.getElementById('city_suggestions').innerHTML = "<table width='100%' cellpading='0' callspacing='1' style='font-family: Verdana; font-size: 10px;'><tr onmouseover=' this.style.background=\"#CECECE\"' onmouseout='this.style.background=\"transparent\"' style='cursor: pointer; cursor: hand;' onclick='setCityName(\"Beograd\");'><td>Beograd</td></tr><tr onmouseover=' this.style.background=\"#CECECE\"' style='cursor: pointer; cursor: hand;' onmouseout='this.style.background=\"transparent\"' onclick='setCityName(\"Kragujevac\");'><td>Kragujevac</td></tr><tr style='cursor: pointer; cursor: hand;' onmouseover=' this.style.background=\"#CECECE\"' onmouseout='this.style.background=\"transparent\"' onclick='setCityName(\"Nis\");'><td>Nis</td></tr></table>";	
}


function setCityName(city)
{
	document.getElementById('city').value = city;
	document.getElementById('city_suggestions').style.display = 'none';
}

function getCitiesFromPHP(city, country) 
{
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    self.xmlHttpReq.open('POST', '/?a=get_cities', true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            document.getElementById('city_suggestions').innerHTML = self.xmlHttpReq.responseText;
        }
    }
    self.xmlHttpReq.send('city='+city+'&country='+country);
}