var MyWin;
function EnlargeImage(url,wid,heg)
{
	wid = 260;
	heg = 200;
	if(MyWin!=null)
	{
		MyWin.close;
	}
				
	MyWin=window.open(url,'images',"height=" + (heg + 200) + ",width=" + (wid + 100) + ",status=no,toolbar=no,menubar=no,location=no,titlebar=no,resizable=no");
	MyWin.moveTo(5,5);
	MyWin.focus();
}

var id_counter=0;
function QuickRequest(lang)
{
	var FullName = document.getElementById("FullName").value;
	var email = document.getElementById("email").value;
	var Address = document.getElementById("Address").value;
	var PostalCode = "NotProvided";
	if(document.getElementById("PostalCode"))
	{
		PostalCode = document.getElementById("PostalCode").value;
	}
	var Telephone = document.getElementById("Telephone").value;	
	var Country = document.getElementById("Country").value;
	var City = document.getElementById("City").value;
	var Comments = ""
	if(document.getElementById("Comments"))
	{
		Comments = document.getElementById("Comments").value;
	}
	var developments = document.getElementById("developments").value;
	var isSubscription = false;
	if(document.getElementById("isSubscription"))
	{
		isSubscription = true;
	}
	
	var emptyFields;
	emptyFields="";
	var InvalidFields;
	InvalidFields="";
	var problem;
	problem="";
	
	if(document.getElementById("FullName").value=="")
	{
		emptyFields+="Surname - Name\n";
	}
	else if(document.getElementById("FullName").value.length < 3)
	{
		InvalidFields+="Surname - Name\n";					
	}
	
	if(document.getElementById("email").value=="")
	{
		emptyFields+="e-mail Address\n";
	}
	else if(document.getElementById("email").value.length < 3)
	{
		InvalidFields+="e-mail Address\n";					
	}
	else if(validateEmail(document.getElementById("email").value)==false)
	{
		InvalidFields+="e-mail Address\n";		
	}
	
	if(emptyFields!="")
	{
		problem+="Please fill in the following fields: \n" + emptyFields;
	}
	
	if(InvalidFields!="")
	{
		if(problem=="")
		{
			problem = "The values of the following fields are not valid:\n" + InvalidFields;
		}
		else
		{
			problem += "and populate the following fields with valid values.\n" + InvalidFields;
		
		}					
	}
	
	if(problem!="")
	{
		document.getElementById("mail_result").style.borderTop = "1px solid #727946";
		document.getElementById("mail_result").innerHTML = '<p style="color:red;text-align:left;margin:10px;">' + problem.replace('\n','<br />') + '</p>';		
	}
	else
	{		
		document.getElementById("mail_result").style.borderTop = "1px solid #727946";
		document.getElementById("mail_result").innerHTML = '<p style="text-align:left;margin:10px;">Sending mail. Please wait ...</p>';	
		var srcString = "container=mail_result";	
		srcString += "&lang="+ lang;
		srcString += "&FullName=" + FullName;
		srcString += "&email=" + email;
		srcString += "&Address=" + Address;
		srcString += "&PostalCode=" + PostalCode;
		srcString += "&Telephone=" + Telephone;
		srcString += "&Country=" + Country;
		srcString += "&City=" + City;
		srcString += "&developments=" + developments;
		srcString += "&Comments=" + Comments;		
		if(isSubscription == true)
		{
			srcString += "&isSubscription=true";		
		}
		
		head = document.getElementsByTagName("head").item(0);
		script = document.createElement("script");
		script.id = "tsouki_" + id_counter;
		id_counter++;
		script.src = "http://www.phoenix-villas.com/inc/quick_enquire_send.asp?" + srcString;
		//alert(script.src);
		script.type = "text/javascript";
		script.defer = true;			
		void(head.appendChild(script));

		if(document.getElementById("sbm"))
		{
			document.getElementById("sbm").disabled="disabled";
		}	
		
	}
}


//Various Useful Functions
function validateEmail(theAddress)
{
	var returnValue = true;
	var AtSym       = theAddress.indexOf('@');
	var Period      = theAddress.lastIndexOf('.');
	var Space       = theAddress.indexOf(' ');
	var Length      = theAddress.length - 1;  // Array is from 0 to length-1

	// '@' cannot be in first position, Must be at least one valid char btwn '@' and '.'
	// Must be at least one valid char after '.', No empty spaces permitted
	if((AtSym < 1) || (Period <= AtSym+1) || (Period == Length ) || (Space  != -1))
		returnValue = false;

	return returnValue;
}

function ValidateContact()
{ 
	//validation against Name, Email, Address & Telephone
	var emptyFields;
	emptyFields="";
	var InvalidFields;
	InvalidFields="";
	var problem;
	problem="";
	
	if(document.getElementById("FullName").value=="")
	{
		emptyFields+="Surname - Name\n";
	}
	else if(document.getElementById("FullName").value.length < 3)
	{
		InvalidFields+="Surname - Name\n";					
	}
	
	if(document.getElementById("email").value=="")
	{
		emptyFields+="e-mail Address\n";
	}
	else if(document.getElementById("email").value.length < 3)
	{
		InvalidFields+="e-mail Address\n";					
	}
	else if(validateEmail(document.getElementById("email").value)==false)
	{
		InvalidFields+="e-mail Address\n";		
	}
	
	if(emptyFields!="")
	{
		problem+="Please fill in the following fields: \n" + emptyFields;
	}
	
	if(InvalidFields!="")
	{
		if(problem=="")
		{
			problem = "The values of the following fields are not valid.\n" + InvalidFields;
		}
		else
		{
			problem += "and populate the following fields with valid values.\n" + InvalidFields;
		
		}					
	}
	
	if(problem!="")
	{
		alert(problem);
		return false;
	}
	
	return true;					
}


function Preview(forsale)
{
	var ThePage;
	ThePage="";
	ThePage=Page;	
	window.frames["SendEmail"].document.open;
	ThePage += window.frames["SendEmail"].document.getElementById("head1").innerHTML;
	ThePage+="</head>";
	if(forsale=="false")
	{
		ThePage+="<body>";
	}
	else
	{
		ThePage+="<body onload=LoadPic('null');Initialize();>";
	}
	ThePage+= window.frames["SendEmail"].document.body.innerHTML;
	window.frames["SendEmail"].document.close;
	ThePage+="</body></html>";
	
	var NewWin;
	NewWin = window.open();
	NewWin.document.open();
	NewWin.document.write(ThePage);
	NewWin.document.close();		
	return false;
}

function Populate(ServerName,forsale)
{
	var ThePage;
	ThePage="";
	ThePage=Page;
	window.frames["SendEmail"].document.open;
	ThePage += window.frames["SendEmail"].document.getElementById("head1").innerHTML;
	ThePage+="</head>";
	if(forsale=="false")
	{
		ThePage+="<body>";
	}
	else
	{
		ThePage+="<body onload=LoadPic('null');Initialize();>";
	}
	ThePage+="<div id='send_friend'>"
	ThePage+= "%mail_info%"
	ThePage+= "</div>"
	ThePage+= window.frames["SendEmail"].document.body.innerHTML;
	window.frames["SendEmail"].document.close;
	ThePage+="</body></html>";
	
	document.getElementById("HTMLbody").value=ThePage;
	return true;			
}


function getCookie(NameOfCookie)
{
	// First we check to see if there is a cookie stored.
	// Otherwise the length of document.cookie would be zero.	
	if (document.cookie.length > 0)
	{	
		// Second we check to see if the cookie's name is stored in the
		// "document.cookie" object for the page.
		
		// Since more than one cookie can be set on a
		// single page it is possible that our cookie
		// is not present, even though the "document.cookie" object
		// is not just an empty text.
		// If our cookie name is not present the value -1 is stored
		// in the variable called "begin".		
		begin = document.cookie.indexOf(NameOfCookie+"=");
		if (begin != -1) // Note: != means "is not equal to"
		{			
			// Our cookie was set.
			// The value stored in the cookie is returned from the function.		
			begin += NameOfCookie.length+1;
			end = document.cookie.indexOf(";", begin);
			if (end == -1) end = document.cookie.length;
		return unescape(document.cookie.substring(begin, end)); 
		}
	}
	return null;
	
	// Our cookie was not set.
	// The value "null" is returned from the function.
}


function setCookie(NameOfCookie, value, expiredays)
{

	// Three variables are used to set the new cookie.
	// The name of the cookie, the value to be stored,
	// and finally the number of days until the cookie expires.
	// The first lines in the function convert
	// the number of days to a valid date.
	
	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));	
	// The next line stores the cookie, simply by assigning
	// the values to the "document.cookie" object.
	// Note the date is converted to Greenwich Mean time using
	// the "toGMTstring()" function.
	
	document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString()) + ";path=/";
}

function delCookie (NameOfCookie)
{
	// The function simply checks to see if the cookie is set.
	// If so, the expiration date is set to Jan. 1st 1970.	
	if (getCookie(NameOfCookie)) 
	{
		document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}


function AddToFavorites(product_id,lang)
{
	//alert(lang);
	var cookie_val = getCookie("Favorites");
	var exists = false;
	if(cookie_val!=null)
	{
		var myArr = cookie_val.split(",");
		for(var j=0;j<myArr.length;j++)
		{
			if(parseInt(myArr[j])==product_id)
			{
				exists = true;
			}
		}
		if(exists==false)
		{
		 cookie_val += "," + product_id;
		}
	}
	else
	{
		cookie_val =  product_id;
	}
	setCookie("Favorites",cookie_val,1000);
	window.frames['listener'].location.href = "/update_favorites.asp?favorites=" + cookie_val + "&lang=" + lang;	
	
}

function RemoveFromFavorites(product_id,lang)
{
	//alert(lang);
	var cookie_val = getCookie("Favorites");
	var new_val = "";
	var k = 0;
	if(cookie_val!=null)
	{
		var myArr = cookie_val.split(",");
		for(var j=0;j<myArr.length;j++)
		{
			if(parseInt(myArr[j])!=product_id)
			{
				if(k>0)
				{
					new_val += ",";
				}
				new_val += myArr[j];
				k += 1;
			}
		}
		
	}	
	setCookie("Favorites",new_val,1000);
	window.frames['listener'].location.href="/update_favorites.asp?favorites=" + new_val + "&lang=" + lang;	
}

function ChangeCategory(source,id)
	{
		//alert(source)
		var li_item;		
		var inner;
		var new_source;
		li_item = document.getElementById(source + "_" + id);
		inner = li_item.innerHTML;
		//alert(inner);
		document.getElementById(source).removeChild(li_item);
		if(source=="outside")
		{
			new_source="inside";
		}
		else
		{
			new_source="outside";
		}
		
		var newLI = document.createElement('li');
		newLI.setAttribute('id',new_source + '_' + id);
		inner = inner.replace(source,new_source);	
		//alert(inner);
		newLI.innerHTML = inner;	
		document.getElementById(new_source).appendChild(newLI);	
		//newA.addEventListener("onclick",ChangeCategory,false);		
	}
	
	function ValidatePortfolio()
	{
		var level_1 = false;
		level_1 = ValidateContact();
		var devs = "";
		if(level_1==true)
		{
			var el_1 = document.getElementById("inside");
			var li_col = el_1.getElementsByTagName("li");
			for(var j=0;j<li_col.length;j++)
			{
				devs += li_col[j].childNodes[0].innerHTML + "<br />";			
			}
			document.getElementById("developments").value = devs;
			return true;
		}
		else
		{
			return false;
		}
	}
	
	

		
	function CheckSize()
	{
		var links_h = parseInt(document.getElementById("about_links").clientHeight) + 40; 
		var about_h = parseInt(document.getElementById("about_us").clientHeight);
		if(links_h - about_h > 0)
		{				
			document.getElementById("about_us").style.height = 	links_h + 5 + 'px';
		}			
	}
	
	var timer;
	var timer_on=false;
	var allIm;
	
	function Initialize()
	{
		if(document.getElementById("thumbs"))
		{
			var currLength = 0;
			var th = document.getElementById("thumbs");
			allIm = th.getElementsByTagName("IMG");
			if(allIm.length>5)
			{
				allowScroll = true;	
				var imgEl;
				for(var t=0;t<allIm.length;t++)
				{
					//alert(document.getElementById("img_" + (t + 1)).src);	
					imgEl = document.getElementById("img_" + (t + 1));
					imgEl.style.position = "absolute";
					imgEl.style.top="0px";			
					imgEl.style.left = currLength - 95 + "px";			
					currLength += 95;			
				}	
				StartScrolling();
			}
			else
			{
				allowScroll = false;	
				var imgEl;
				for(var t=0;t<allIm.length;t++)
				{
					imgEl = document.getElementById("img_" + (t + 1));
					imgEl.style.position = "absolute";
					imgEl.style.top="0px";		
					currLength += 95;		
					imgEl.style.left = currLength - 95 + "px";								
				}			
			}
		}
	}	
	
	function StartScrolling()
	{
		if(allowScroll)
		{
			if(timer_on==false)
			{
				timer=setTimeout("scroll();",15);
				timer_on=true;
			}
		}
	}
	
	function scroll()
	{
		var i;
		var temp;
		for(i=0;i<allIm.length;i++)
		{	
		
			temp = parseInt(allIm[i].style.left) 			
			allIm[i].style.left =  (temp + 1) + "px";
			temp = parseInt(allIm[i].style.left);			
			if(parseInt(allIm[i].style.left)== (allIm.length-1)*95)
			{
				allIm[i].style.left= "-95px"	
				//alert((allIm.length-1)*95);				
			}
		}
		timer=setTimeout("scroll();",15);
	}
	
	function StopScrolling()
	{
		if(allowScroll)
		{
			if(timer_on==true)
			{
				clearTimeout(timer);
				timer_on=false;
			}
		}
	}

	
function LoadPic(file_index)
{	
	if(document.getElementById("about_logo"))
	{
		var file_url;
		
		if(file_index=="0")
		{
			document.getElementById("about_logo").src = imageDB[0].src;
		}
		else
		{
			file_url = "/files/" + strIm[parseInt(file_index,10)-1];
			document.getElementById("about_logo").src = file_url;
			if(document.getElementById("about_logo_description"))
			{
				if(descrArray[imgIds[parseInt(file_index,10)-1]] != "")
				{
					document.getElementById("about_logo_description").innerHTML= "<p>" + descrArray[imgIds[parseInt(file_index,10)-1]] + "</p>"; 
					document.getElementById("about_logo_description").style.display="";
				}
				else
				{
					document.getElementById("about_logo_description").style.display="none";
				}
			}
		}	
		
	}
}	
