var foto_response = null;
var auth_hash = null;
var error_message = null;
var proxy_throw = "/sites/all/themes/ecoton/ged.fotoapi.proxy.php"; // Add your proxy type here!

function Authorize(){	
	var soapBody = new SOAPObject("Authorize"); //Create a new object that we want to send to a web service
	soapBody.ns = "http://www.gdeetotdom.ru/";
	soapBody.appendChild(new SOAPObject("login")).val("#LOGIN#");
	soapBody.appendChild(new SOAPObject("password")).val("#PASSWORD#");
	soapBody.appendChild(new SOAPObject("life_time")).val(30);
	//Create a new SOAP Request
	var sr = new SOAPRequest("http://www.gdeetotdom.ru/Authorize", soapBody); //Request is now ready to be sent to a web-service
	//Lets send it
	SOAPClient.Proxy = proxy_throw; //Specify web-service address (if local to your domain) or a proxy file
	SOAPClient.SOAPServer = "http://www.gdeetotdom.ru/foto_api/action.php"; //My proxy uses SOAPServer header to redirect my requests	
	SOAPClient.SendRequest(sr, onResponse); //Send request to server and assign callback function
	if ((foto_response==null) || !foto_response.Body || !foto_response.Body[0] || foto_response.Body[0].Fault){
		if ((foto_response!=null) && foto_response.Body && foto_response.Body[0] && foto_response.Body[0].Fault){
			// alert(foto_response.Body[0].Fault[0].faultstring[0].Text)
		}
		return false;
	}
	auth_hash = foto_response.Body[0].AuthorizeResponse[0].sess_info[0].sess_id[0].Text;
	var life_time = foto_response.Body[0].AuthorizeResponse[0].sess_info[0].life_time[0].Text;
	setTimeout('Authorize()', (life_time-1)*60*1000);
	return auth_hash;
}

function GetHouseURLByAddress(address){
	if (auth_hash == null || auth_hash.length<=0) {
		if (Authorize()==false){
			return false;
		}
	}
	if (auth_hash == null || auth_hash.length<=0) {
		return false;
	}
	var soapBody = new SOAPObject("GetHouseURLByAddress"); //Create a new object that we want to send to a web service
	soapBody.ns = "http://www.gdeetotdom.ru/";
	soapBody.appendChild(new SOAPObject("sess_id")).val(auth_hash);
	soapBody.appendChild(new SOAPObject("address")).val(address);

	//Create a new SOAP Request
	var sr = new SOAPRequest("http://www.gdeetotdom.ru/GetHouseURLByAddress", soapBody); //Request is now ready to be sent to a web-service
	//Lets send it
	SOAPClient.Proxy = proxy_throw; //Specify web-service address (if local to your domain) or a proxy file
	SOAPClient.SOAPServer = "http://www.gdeetotdom.ru/foto_api/action.php"; //My proxy uses SOAPServer header to redirect my requests	
	SOAPClient.SendRequest(sr, onResponse); //Send request to server and assign callback function
	if ((foto_response==null) || !foto_response.Body || !foto_response.Body[0] || foto_response.Body[0].Fault) {
		if (foto_response.Body[0].Fault) {
			error_message = foto_response.Body[0].Fault[0].faultstring[0].Text;
			return false;
		}
		return false;
	}
	return foto_response.Body[0].GetHouseURLByAddressResponse[0].house_url[0].Text;
}

function GetHouseURLByGeo(longtitude, latitude){
	if (auth_hash == null || auth_hash.length<=0) {
		if (Authorize()==false){
			return false;
		}
	}
	if (auth_hash == null || auth_hash.length<=0) {
		return false;
	}
	var soapBody = new SOAPObject("GetHouseURLByGeo"); //Create a new object that we want to send to a web service
	soapBody.ns = "http://www.gdeetotdom.ru/";
	soapBody.appendChild(new SOAPObject("sess_id")).val(auth_hash);
	soapBody.appendChild(new SOAPObject("longtitude")).val(longtitude);
	soapBody.appendChild(new SOAPObject("latitude")).val(latitude);

	//Create a new SOAP Request
	var sr = new SOAPRequest("http://www.gdeetotdom.ru/GetHouseURLByGeo", soapBody); //Request is now ready to be sent to a web-service
	//Lets send it
	SOAPClient.Proxy = proxy_throw; //Specify web-service address (if local to your domain) or a proxy file
	SOAPClient.SOAPServer = "http://www.gdeetotdom.ru/foto_api/action.php"; //My proxy uses SOAPServer header to redirect my requests	
	SOAPClient.SendRequest(sr, onResponse); //Send request to server and assign callback function
	if (!foto_response.Body || !foto_response.Body[0] || foto_response.Body[0].Fault){
		if (foto_response.Body[0].Fault){
			error_message = foto_response.Body[0].Fault[0].faultstring[0].Text;
			return false;
		}
		return false;
	}
	return foto_response.Body[0].GetHouseURLByGeoResponse[0].house_url[0].Text;
}


function onResponse(res){
	foto_response = res;
}
