//////////////////////////////////////////////////////////////////////////////////////////////////////////////
var xmlHttp = createXmlHttpRequestObject();
var currType = "";
var addressStatus = 0;
var ownerStatus = 0;
var isEditPage = 0;
var curPos = 0;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function createXmlHttpRequestObject()
{
	var xmlHttp;
	if(window.ActiveXObject)
	{
		try
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp = false;
		}
	}
	else
	{
		try
		{
			xmlHttp = new XMLHttpRequest();
		}
		catch (e)
		{
			xmlHttp = false;
		}
	}

	if (!xmlHttp)
		alert("Error creating the XMLHttpRequest object.");
	else
		return xmlHttp;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function OnSelected(type)
{	
	AddressChanged();
	currType = type;
	// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{	
		var country = document.getElementById("country").value;	
		var state = document.getElementById("state").value;
		var url = "postcommunity_svr.php?type=" + type +			   
			   "&country="+ country +  
			   "&state="+ state +
			   "&city="+
			   "&address=";
		//alert(url);
		//postcommunity_svr.php?type=countrySeleted&country=TH&state=&city=&address=
		xmlHttp.open("GET", url, true);
		xmlHttp.onreadystatechange = handleSvrRespOnSelected;
		xmlHttp.send(null);
	}
	else
	{	
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function handleSvrRespOnSelected()
{
	if (xmlHttp.readyState == 4)
	{		
		// parse string result and update Elements
		var strReponse = xmlHttp.responseText;
		//alert(strReponse);	
		if(currType == "countrySelected")
		{
			if(strReponse.indexOf("error") < 0)
			{
				var tempArr = strReponse.split("|");			
				
				document.getElementById("stateContent").innerHTML = tempArr[0];
				//alert(tempArr[1]);	
				document.getElementById("cityContent").innerHTML = tempArr[1];
			}
			else
			{
				document.getElementById("stateContent").innerHTML = '<input type="text" class="txt_name" name="state" id="state"/>';
				document.getElementById("cityContent").innerHTML = '<input type="text" class="txt_name" name="city" id="city" />';
			}
		}
		else
		{
			if(strReponse.indexOf("error") < 0){			
				document.getElementById("cityContent").innerHTML = strReponse;
			}
			else {
				document.getElementById("cityContent").innerHTML = '<input type="text" class="txt_name" id="city" name="city"/>';
			}
		}
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function ValidateOwner(ownername)
{
	//alert(ownername);
	// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{	
		var country = document.getElementById("country").value;	
		var state = document.getElementById("state").value;
		var url = "postcommunity_svr.php?type=checkOwnerExisted" +
			   "&country="+ ownername +  
			   "&state="+
			   "&city="+
			   "&address=";
		//alert(url);
		//postcommunity_svr.php?type=countrySeleted&country=TH&state=&city=&address=
		xmlHttp.open("GET", url, true);
		xmlHttp.onreadystatechange = handleSvrRespCheckOwner;
		xmlHttp.send(null);
	}
	else
	{	
		document.getElementById("ownerErr").innerHTML = "Cannot connect server.";	
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function handleSvrRespCheckOwner()
{
	if (xmlHttp.readyState == 4)
	{		
		// parse string result and update Elements
		var strReponse = xmlHttp.responseText;
		//alert(strReponse);
		if(strReponse.indexOf("valid") >= 0)
		{
			document.getElementById("ownerErr").innerHTML = "Owner is valid";
			ownerStatus = 3; // valid
		}
		else if (strReponse.indexOf("existed") >= 0)
		{
			document.getElementById("ownerErr").innerHTML = "Owner name is not available, please choose another name.";
			ownerStatus = 2; // Invalid
		}
		else
		{
			//document.getElementById("ownerErr").innerHTML = "Validation owner was failed.";
			ownerStatus = 2; // Invalid
		}		
	}
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//				VALIDATION
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function AddressChanged()
{
	addressStatus = 0;
	document.getElementById("addErr").style.display = "block";
	document.getElementById("addErr").innerHTML = "Invalid address.";
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function CheckAddress()
{
	addressStatus = 1 // Move to checking status
	document.getElementById("addErr").style.display = "block";
	var addr = document.form.address.value + ' ' + 
			   document.form.city.value + ' ' + 
			   document.form.state.value + ' ' + 
			   document.form.zip.value + ' ' + 
			   document.form.country.value;
			   
	document.getElementById("addErr").innerHTML = "Finding address '" + addr + "'";
	//alert(addr);
	ValidateAddress(addr);
	return false;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function ZipChanged()
{
	if(document.form.zip.value.trim() != "")
	{
		if (!isZIP(document.form.zip)) 
			document.getElementById("zipErr").style.display = "block";
		else
			document.getElementById("zipErr").style.display = "none";
	}
	else
	{
		document.getElementById("zipErr").style.display = "none";
	}
	AddressChanged();
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function ContactnameChanged()
{
	if (!isAlphaSpace(document.form.contactName)) 
		document.getElementById("contactNameErr").style.display = "block";
	else
		document.getElementById("contactNameErr").style.display = "none";
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function TelephoneChanged()
{
	if (!isValidNumber(document.form.telephone.value)) 
		document.getElementById("telephoneErr").style.display = "block";
	else
		document.getElementById("telephoneErr").style.display = "none";
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function Faxchanged()
{
	if (!isValidNumber(document.form.telephone.value)) 
		document.getElementById("faxErr").style.display = "block";
	else
		document.getElementById("faxErr").style.display = "none";
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function Faxchanged()
{
	if(document.form.fax.value.trim() != "")
	{	
		if (!isValidNumber(document.form.fax.value)) 
			document.getElementById("faxErr").style.display = "block";
		else
			document.getElementById("faxErr").style.display = "none";
	}
	else
	{
		document.getElementById("faxErr").style.display = "none";
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function EmailChanged()
{	
	if (!validateEmail(document.form.email.value)) 
		document.getElementById("emailErr").style.display = "block";
	else
		document.getElementById("emailErr").style.display = "none";	
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function OwnerChanged()
{
	document.getElementById("ownerErr").style.display = "block";
	ownerStatus = 1 // Move to checking status
	document.getElementById("ownerErr").innerHTML = "Checking...";
	var ownername = document.form.owner.value.trim();
	ValidateOwner(ownername);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function PasswordChanged()
{
	if (!isNonEmpty(document.form.password) || !isNonEmpty(document.form.password_rt)) 
	{
		document.getElementById("passwordErr").style.display = "block";				
		return;
	}
	
	if (document.form.password.value.trim() != document.form.password_rt.value.trim()) 
	{	
		document.getElementById("passwordErr").style.display = "block";	
		return;
	}
	
	document.getElementById("passwordErr").style.display = "none";
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function ViewAddress()
{
	var lat = document.form.latitude.value;
	var lon = document.form.longitude.value;
	window.open('viewmap.php?lat='+ lat + '&lon=' + lon);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function GetAvaiablePhotoPosition()
{
	if(photo1 == 0) { photo1=1; return 1;}
	if(photo2 == 0) { photo2=1; return 2;}
	if(photo3 == 0) { photo3=1; return 3;}
	if(photo4 == 0) { photo4=1; return 4;}
	if(photo5 == 0) { photo5=1; return 5;}
	if(photo6 == 0) { photo6=1; return 6;}
	return 0;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function UploadImage()
{
	if(document.getElementById("fileField").value != "")
	{
		var avaiablePostion = GetAvaiablePhotoPosition();
		if(avaiablePostion  == 0){ 
			alert("You have uploaded 6 images.");
		}else {				
			//alert(avaiablePostion);
			//document.uploadform.action="upload.php?cur=" + avaiablePostion;
			document.getElementById("curr").innerHTML = avaiablePostion;
			document.getElementById("uploadform").action= "upload.php?cur=" + avaiablePostion;
			document.getElementById("uploadform").submit();
		}
	}
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function ViewImage(imageURL) 
{
	var w = 450;
	var	h = 350;
 	var left = (screen.width/2)-(w/2);
	var top = (screen.height/2)-(h/2);
	var targetWin = window.open('viewimage.php?img=' + imageURL,'Image','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function ResetImage(curr)
{
	//<dd id="imgUpload5">
	var str = 'imgUpload' + curr;
	var str1 = 'photo_'+ curr;
	//alert(str);
	document.getElementById(str).innerHTML = "no image";
	document.getElementById(str1).value = "";
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function DeleteImage(strImage, curr)
{
	if(curr == 1) 
		photo1 = 0;
	else if (curr == 2)
		photo2 = 0;
	else if (curr == 3)
		photo3 = 0;
	else if (curr == 4)
		photo4 = 0;
	else if (curr == 5)
		photo5 = 0;
	else
		photo6 = 0;	
	
	ResetImage(curr);
	
	//alert('Delete image ' + strImage);
	
	// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{		
		var url = "delete.php?path=" + strImage + "&curr=" + curr;
		//alert(url);
		//delete.php?path=upload/1.JPG&curr=2
		xmlHttp.open("GET", url, true);
		xmlHttp.onreadystatechange = handleSvrRespDeletedFile;
		xmlHttp.send(null);
	}
	else
	{	
	}
}

////////////////////////////////////////////////////////////////////////////////////
function handleSvrRespDeletedFile()
{
	if (xmlHttp.readyState == 4)
	{		
		// parse string result and update Elements
		var strReponse = xmlHttp.responseText;
		alert(strReponse);
	}
}