//AJAX Functions
//<link href="../css/wgmain.css" rel="stylesheet" type="text/css" />

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 sendGetRequest(url, func) 
{
    var oXmlHttp = createXMLHttp();
    oXmlHttp.open("get", url, true);

    oXmlHttp.onreadystatechange = function () 
	{
        if (oXmlHttp.readyState == 4) {

            if (oXmlHttp.status == 200) {
			       	var result = JSON.parse(oXmlHttp.responseText);
                eval(func + "(result);");
            } else {
                alert("An error occurred: "+ oXmlHttp.statusText);
            }
        }
    };
    oXmlHttp.send(null);
}


function getRegions(id)
{
	sendGetRequest('http://www.em-stroy.com/server/get_regions.php?id=' + id, 'displayRegions');	
}



function myfunc()
{
	alert("ilcia");	
}


function getSmallThumbs(id,ord,param)
{ 

	sendGetRequest('http://www.em-stroy.com/server/get_small_thumbs.php?id='+id+'&ord='+ord+'&param='+param, 'showSmallThumbs');	
}



function silvia()
{
	var a='';
	return a;
}

function Myval(aResult)
{
	a = parseInt(aResult)
	//alert(a);
	return a;
	
}

function getSubtypes(id)
{
	sendGetRequest('server/get_subtypes.php?id=' + id, 'displaySubtypes');	
}



function showSmallThumbs(aSmallTh)
{
	some=aSmallTh[0];
	if (some.param==2) {var div_exc  = document.getElementById('small_pictures');}
	if (some.param==3) {var div_exc  = document.getElementById('small_pictures_plan'); }
	data='';
	
	if (aSmallTh.length>0)
	{
		aa=aSmallTh[0];
		
		new Effect.Opacity(div_exc, Object.extend(
                                   {
                                        duration: 0.5, 
                                        from:1.0, 
                                        to:0.0,
                                        afterFinishInternal: function(effect) 
                                        {
                                            showSmallThumbsAgain(aSmallTh);
											Effect.Appear(div_exc,  Object.extend({duration: 0.5}));
                                        }
                                   }
                                ));
		
	}
}

function showSmallThumbsAgain(aSmallTh)
{
	some=aSmallTh[0];
	
	if (some.param==2) {var div_small  = document.getElementById('small_pictures');scrol='gallery_scroll';}
	if (some.param==3) {var div_small  = document.getElementById('small_pictures_plan'); scrol='plan_scroll';}
	
	data='';
	if (aSmallTh.length>0)
	{
		
		for (var i=0; i < aSmallTh.length; i++) 
		{
			some=aSmallTh[i];
			
			data = data +'<a onclick="showBigThumb(\''+ some.p1 +'\',\''+ some.param +'\',\''+ some.bigg +'\',\''+ some.title +'\')" style="cursor:pointer;">';
			data = data +'<img src="'+ some.p +'" width="87" height="57" border="0" id="'+ some.id +'" class="gallery_small_img"/></a>';
			
			if (i==aSmallTh.length-1) 
			{
				
				data = data +'<input type="hidden" id="firstNumber" value="'+some.h+'">';
				data = data +'<input type="hidden" id="all_rows" value="'+some.all_rows+'">';
				PrevNext(some.h,some.all_rows,scrol,some.prid,some.param);
				
				
			}
			
			

		}
		
	}
	
	div_small.innerHTML = data;
}


function PrevNext(id,all_rows,divv,prid,param)
{
	divv= document.getElementById(divv);
	data_s ='';
	if (all_rows>6)
		{
			
			y=parseInt(id)+6;
			yy=parseInt(id)-6;
			if (yy>=0) link_minus='getSmallThumbs('+prid+','+yy+','+param+')';
			else link_minus='';
			
			if (y < all_rows) link_plus='getSmallThumbs('+prid+','+y+','+param+')';
			else link_plus='';
			data_s = data_s +'<a onclick="'+ link_minus+'" style="cursor:pointer"> << </a><a onclick="'+link_plus+'" style="cursor:pointer"> >> </a>';
			
		}
		else data_s = data_s +'<a onclick="" style="cursor:pointer"> << </a><a onclick="" style="cursor:pointer"> >> </a>';
	divv.innerHTML = data_s;
}

function SetBigThumb(image,par,bigg,title)
{
	if (par==1) {var div_big  = document.getElementById('big_picture'); cla="gallery"; i="BigThumb";}
	if (par==2) {var div_big  = document.getElementById('big_gallery'); cla="gallery"; i="BigThumb_galley";}
	if (par==3) {var div_big  = document.getElementById('big_plans'); cla="Floorplans"; i="BigThumb_plan";}
	if (par==4) {var div_big  = document.getElementById('big_map'); cla="locAndMaps"; i="BigThumb_map";}
	
	data='';
	data = data +'<a href="'+bigg+'" class="'+cla+'" onclick="myLightbox.start(this); return false;" rel="lightbox[roadtrip]"><img src="'+image+'" width="270" height="215" border="0" id="'+i+'" alt ="'+title+'" /></a>';
	
	div_big.innerHTML = data;
}

function showBigThumb(image,par,bigg,title)
{
	
	if (par==1) {var div_big  = document.getElementById('big_picture'); cla="gallery"; i="BigThumb";}
	if (par==2) {var div_big  = document.getElementById('big_gallery'); cla="gallery"; i="BigThumb_galley";}
	if (par==3) {var div_big  = document.getElementById('big_plans'); cla="Floorplans"; i="BigThumb_plan";}
	if (par==4) {var div_big  = document.getElementById('big_map'); cla="locAndMaps"; i="BigThumb_map";}
	
		new Effect.Opacity(div_big, Object.extend(
                                   {
                                        duration: 0.5, 
                                        from:1.0, 
                                        to:0.0,
                                        afterFinishInternal: function(effect) 
                                        {
                                            SetBigThumb(image,par,bigg,title);
											Effect.Appear(div_big,  Object.extend({duration: 0.5}));
                                        }
                                   }
                                ));
	
}



function displaySubtypes(aRegions)
{
	var div  = document.getElementById('subtype_container');
	var init = document.getElementById('subtype_init').value;
	
	
	if(aRegions.length>0)
	{
		var sel  = '<select name="subtype_id" id="idsubtype_id" style=" width:100px;">';
	
		for (var i=0; i < aRegions.length; i++) 
		{
			some = aRegions[i];
			sel += '<option value="' + some.id + '"';
			if(some.id == init)
				sel += " selected";
		
			sel += '>' + some.name + '</option>';
   		}
	
		sel += '</select>';
	}
	else sel = "";
	div.innerHTML = sel;
}

function displayRegions(aRegions)
{
	var div  = document.getElementById('region_container');
	var init = document.getElementById('region_init').value;
	
	
	var sel  = '<select name="regionid" id="idregion_id" class="sel" style=" width:100px;">';
	
	for (var i=0; i < aRegions.length; i++) 
	{
		some = aRegions[i];
		sel += '<option value="' + some.id + '"';
		if(some.id == init)
			sel += " selected";
		
		sel += '>' + some.name + '</option>';
    }
	
	sel += '</select>';
	
	div.innerHTML = sel;
}


function ShowTabs(par,image,real_image,id,title)
{
	
	if (par==1)
	{
		
		document.getElementById('big_picture').innerHTML='';
		document.getElementById('gallery').style.display="none";
		document.getElementById('plan').style.display="none";
		document.getElementById('mapp').style.display="none";
		document.getElementById('general').style.display="block";
		showBigThumb(image,'1',real_image,title);
		
	}
	if (par==2)
		{
		
		document.getElementById('big_gallery').innerHTML='';
		document.getElementById('small_pictures').innerHTML='';
		document.getElementById('general').style.display="none";
		document.getElementById('plan').style.display="none";
		document.getElementById('mapp').style.display="none";
		document.getElementById('gallery').style.display="block";
		showBigThumb(image,'2',real_image,title);
		getSmallThumbs(id,0,2);
	}
	if (par==3)
		{
		
		document.getElementById('big_plans').innerHTML='';
		document.getElementById('small_pictures_plan').innerHTML='';
		document.getElementById('general').style.display="none";
		document.getElementById('gallery').style.display="none";
		document.getElementById('mapp').style.display="none";
		document.getElementById('plan').style.display="block";
		showBigThumb(image,'3',real_image,title);
		getSmallThumbs(id,0,3);
	}
	if (par==4)
		{
		
		document.getElementById('big_map').innerHTML='';
		document.getElementById('general').style.display="none";
		document.getElementById('gallery').style.display="none";
		document.getElementById('plan').style.display="none";
		document.getElementById('mapp').style.display="block";
		showBigThumb(image,'4',real_image,title);
		
	}
	
		

}

function showStyle(par,id)
{
	
	if (id=='l_1') 
	{ 
	 if (par==1)
	 	 document.getElementById('l_1').className="my_yellow_button_over"; 
	 else  document.getElementById('l_1').className="my_yellow_button"; 
	  document.getElementById('l_2').className="my_yellow_button";
	  document.getElementById('l_3').className="my_yellow_button";
	  document.getElementById('l_4').className="my_yellow_button";
	}	
	
	if (id=='l_2') 
	{ 
	 if (par==1)
	 	 document.getElementById('l_2').className="my_yellow_button_over"; 
	 else  document.getElementById('l_2').className="my_yellow_button"; 
	  document.getElementById('l_1').className="my_yellow_button";
	  document.getElementById('l_3').className="my_yellow_button";
	  document.getElementById('l_4').className="my_yellow_button";
	}
	
	if (id=='l_3') 
	{ 
	 if (par==1)
	 	 document.getElementById('l_3').className="my_yellow_button_over"; 
	 else  document.getElementById('l_3').className="my_yellow_button"; 
	  document.getElementById('l_2').className="my_yellow_button";
	  document.getElementById('l_1').className="my_yellow_button";
	  document.getElementById('l_4').className="my_yellow_button";
	}
	
	if (id=='l_4') 
	{ 
	 if (par==1)
	 	 document.getElementById('l_4').className="my_yellow_button_over"; 
	 else  document.getElementById('l_4').className="my_yellow_button"; 
	  document.getElementById('l_2').className="my_yellow_button";
	  document.getElementById('l_3').className="my_yellow_button";
	  document.getElementById('l_1').className="my_yellow_button";
	}
}

//-------------------
/// ALL THING YOU NEED FOR REQUEST FORMSs

	var div1 = "";


function openRequest()
{ 
	div1 = document.getElementById('request_info_new');
 
	div1.style.display = "block";
	div1.style.top =  arguments[1] + "px";
	div1.style.left = arguments[0] + "px";
	
	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['request_name'].value == "") 
	{
		alert("Please input Your Name.!");
		document.forms['request'].elements['request_name'].focus();
		return false;
	}
	if (document.forms['request'].elements['request_phone'].value == "") 
	{
		alert("Please input Your Phone Number.!");
		document.forms['request'].elements['request_phone'].focus();
		return false;
	}
	if (document.forms['request'].elements['request_email'].value == "" || echeck(document.forms['request'].elements['request_email'].value)==false) 
	{
		alert("Please select correct e-mail address.!");
		document.forms['request'].elements['request_email'].value = "";
		document.forms['request'].elements['request_email'].focus();
		return false;
	}
	
	if (document.forms['request'].elements['about'].value == "") 
	{
		alert("Please input Your Phone Number.!");
		document.forms['request'].elements['about'].focus();
		return false;
	}
	
	checkCode(document.forms['request'].elements['code'].value);
	a= Myval();
	alert("Message is sending");
	//alert(a);
	if ((a==0) || (!a)) 
	{
		alert("The code is not correct");
		document.forms['request'].elements['email'].focus();
		return false;
	}
	
							
	return true
}


function validate2()
{
	if (document.forms['write'].elements['name'].value == "") 
	{
		alert("Please input Your Name.!");
		document.forms['write'].elements['name'].focus();
		return false;
	}

	if (document.forms['write'].elements['email'].value == "" || echeck(document.forms['write'].elements['email'].value)==false) 
	{
		alert("Please select correct e-mail address.!");
		document.forms['write'].elements['email'].value = "";
		document.forms['write'].elements['email'].focus();
		return false;
	}
							
	return true
}



function CheckAndGo()
{
	var checked = false;
	checked = validate();
	if (checked) 
	{
		//fader.fadeOut();
		sendRequest('request');
		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;
}


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 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( formm) 
{
	  
    var oForm = document.forms[formm];
    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 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 number_format(a, b, c, d) {
	// number_format(number, decimals, comma, formatSeparator)
	a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
	e = a + '';
	f = e.split('.');
	if(!f[0]) f[0] = '0';
	if(!f[1]) f[1] = '';
	if(f[1].length < b){
		g = f[1];
		for(i = f[1].length + 1; i <= b; i++) {
			g += '0';
		}
		f[1] = g;
	}
	if(d != '' && f[0].length > 3) {
		h = f[0];
		f[0] = '';
		for(j = 3; j < h.length; j += 3) {
			i = h.slice(h.length - j, h.length - j + 3);
			f[0] = d + i +  f[0] + '';
		}
		j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
		f[0] = j + f[0];
	}
	c = (b <= 0) ? '': c;
	return f[0] + c + f[1];
}
