///
/// ALL THING YOU NEED FOR REQUEST FORMSs

	var div1 = "";


function openRequest()
{
	div1 = document.getElementById('div1');
	div1.style.display = "block";
	div1.style.top = arguments[1] + "px";
	div1.style.left = arguments[0] + "px";
	
	if(arguments[2])
		injectInput('request_id', arguments[2]);
	
	if(arguments[3])
		injectInput('request_about', arguments[3]);
	
	fader.fadeIn();
	fader2.fadeIn();
}

function openDesc()
{
	div1 = document.getElementById('divDescription');
	div1.style.display = "block";
	
	if (window.innerWidth || window.innerHeight)
	{ 
		docwidth = window.innerWidth; 
		docheight = window.innerHeight; 
	} 
	//IE Mozilla 
	if (document.body.clientWidth || document.body.clientHeight)
	{ 
		docwidth = document.body.clientWidth; 
		docheight = document.body.clientHeight; 
	} 
	
	if((docheight - 213)<arguments[1])
		div1.style.top  = (docheight - 200) + "px";
	else 	
		div1.style.top  = (arguments[1] - 10) + "px";
		
	div1.style.left = (arguments[0] + 105) + "px";	
	
	injectDiv('descTitle', arguments[2]);
	injectDiv('moreTitle', arguments[3]);
	injectInput('id', arguments[4]);
	injectDiv('listName', arguments[5]);
	injectDiv('listDesc', arguments[7]);
	injectDiv('descRegion', arguments[8]);
	injectDiv('listRegion', arguments[9]);
	
	document.getElementById('listImg').src = arguments[6] + ".jpg";
}

function closeDesc()
{
	document.getElementById('divDescription').style.display = "none";	
}

function injectDiv(some_id, text_to_inject)
{
	var id_container = document.getElementById(some_id);
	id_container.innerHTML = text_to_inject;
}

function injectInput(some_id, text_to_inject)
{
	var id_container = document.getElementById(some_id);
	id_container.value = text_to_inject;
}

function closeRequest()
{
	fader.fadeOut();
	fader2.fadeOut();
}

function validate()
{
	if (document.forms['request'].elements['name'].value == "") 
	{
		alert("Please select Name.!");
		document.forms['request'].elements['name'].focus();
		return false;
	}
	if (document.forms['request'].elements['last_name'].value == "") 
	{
		alert("Please select Last name.!");
		document.forms['request'].elements['last_name'].focus();
		return false;
	}
	if (document.forms['request'].elements['phone'].value == "") 
	{
		alert("Please select Phone.!");
		document.forms['request'].elements['phone'].focus();
		return false;
	}
	if (document.forms['request'].elements['email'].value == "" || echeck(document.forms['request'].elements['email'].value)==false) 
	{
		alert("Please select correct e-mail address.!");
		document.forms['request'].elements['email'].value = "";
		document.forms['request'].elements['email'].focus();
		return false;
	}
							
	return true
}

function CheckAndGo()
{
	var checked = false;
	checked = validate();
	if (checked) 
	{
		fader.fadeOut();
		sendRequest();
		document.forms['request'].reset();
		closeRequest();
	}
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
			
						
						
						
//AJAX Functions
function createXMLHttp() 
{

    if (typeof XMLHttpRequest != "undefined") 
	{
        return new XMLHttpRequest();
    } 
	else if (window.ActiveXObject) 
	{
      	var aVersions = [ "MSXML2.XMLHttp.5.0",
        "MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
       	"MSXML2.XMLHttp","Microsoft.XMLHttp"
      	];

      	for (var i = 0; i < aVersions.length; i++) 
		{
        	try 
			{
            	var oXmlHttp = new ActiveXObject(aVersions[i]);
           		return oXmlHttp;
        	} 
			catch (oError) 
			{
            	//Do nothing
        	}
      	}
   	}
    throw new Error("XMLHttp object could be created.");
}

						
function getRequestBody(oForm) 
{
    var aParams = new Array();

    for (var i=0 ; i < oForm.elements.length; i++) 
	{
        var sParam = encodeURIComponent(oForm.elements[i].name);
        sParam += "=";
        sParam += encodeURIComponent(oForm.elements[i].value);
        aParams.push(sParam);
    }

    return aParams.join("&");
}



						
function sendRequest() 
{
    var oForm = document.forms['request'];
    var sBody = getRequestBody(oForm);

    var oXmlHttp = createXMLHttp();
    oXmlHttp.open("post", oForm.action, true);
   	oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    oXmlHttp.onreadystatechange = function () 
	{
        if (oXmlHttp.readyState == 4) 
		{
            if (oXmlHttp.status == 200) 
			{
                saveResult(oXmlHttp.responseText);
            } 
			else 
			{
                saveResult("An error occurred: "+ oXmlHttp.statusText);
           	}
        }
    };
    oXmlHttp.send(sBody);
}


function saveResult(msg)
{
	//alert(msg);
}	

function sendMailRequest(formName) 
{
    var oForm = document.forms[formName];
    var sBody = getRequestBody(oForm);

    var oXmlHttp = createXMLHttp();
    oXmlHttp.open("post", oForm.action, true);
   	oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    oXmlHttp.onreadystatechange = function () 
	{
        if (oXmlHttp.readyState == 4) 
		{
            if (oXmlHttp.status == 200) 
			{
                clearForm(oXmlHttp.responseText, formName);
            } 
			else 
			{
                clearForm("An error occurred: "+ oXmlHttp.statusText, formName);
           	}
        }
    };
    oXmlHttp.send(sBody);
}

function clearForm(msg, formName)
{
	document.forms[formName].reset();
	//alert(msg);
}





/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    //alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}

/*function ValidateForm(){
	var emailID=document.frmSample.txtEmail
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }*/




function checkdateformat(userinput)
{
	var dateformat = /^\d{1,2}(\-)\d{1,2}\1\d{1,2}$/   //format dd-mm-yy
	return dateformat.test(userinput) //returns true or false depending on userinput
}



function showhide(d) 
{
	if (d.style.display=="none") 
	{ 
		d.style.display=""; d.style.visibility="visible"; 
	}
	else 
		d.style.display="none";
}

function showSaleSearch()
{
	if(document.getElementById('rentSearch').style.display != "none")
		showRentSearch();
	showhide(document.getElementById('saleSearch'));
	showhide(document.getElementById('leftSale'));	
	showhide(document.getElementById('rightSale'));	
}

function showRentSearch()
{
	if(document.getElementById('saleSearch').style.display != "none")
		showSaleSearch();
	showhide(document.getElementById('rentSearch'));
	showhide(document.getElementById('leftRent'));	
	showhide(document.getElementById('rightRent'));	
}
	

function rand ( n )
{
  	return ( Math.floor ( Math.random ( ) * n + 1 ) );
}

function modul (num, mod)
{
	if(num>mod)
		return num - mod;
	else 
		return num;
}

