// JavaScript Document
//-----------------------------------------------------------------------------------
// AJAX
// holds an instance of XMLHttpRequest
//-----------------------------------------------------------------------------------
var xmlHttp = createXmlHttpRequestObject();
var xhrOk = true;
var _trName = '';
var _tdName = '';
// creates an XMLHttpRequest instance
function createXmlHttpRequestObject()
{
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	// this should work for all browsers except IE6 and older
	try
	{
		// try to create XMLHttpRequest object
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		// assume IE6 or older
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
						"MSXML2.XMLHTTP.5.0",
						"MSXML2.XMLHTTP.4.0",
						"MSXML2.XMLHTTP.3.0",
						"MSXML2.XMLHTTP",
						"Microsoft.XMLHTTP");
		// try every prog id until one works
		for (var i = 0; i < XmlHttpVersions.length && !xmlHttp; i++)
		{
			try
			{
				// try to create XMLHttpRequest object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch (e)
			{

			}
		}
	}
	// return the created object or display an error message
	if (!xmlHttp)
	{
		// alert("Error creating the XMLHttpRequest object.");
	}
	else return xmlHttp;
}

//---------------------------------------
// show/hide table row - langxangvn VinaCyber JSC 10/2006
// TR visibility
//---------------------------------------
function showTR(idTR, bVisibility)
{
	if(navigator.appName.indexOf("Microsoft") > -1)
	{
		// neu la IE thi visibility la block
		var canSee = 'block';
	}
	else
	{
		// FF table-row
		var canSee = 'table-row';
	}

	if (document.getElementById && document.createTextNode)
	{
		var tr = document.getElementById(idTR);
		if (tr)
		{
			if(bVisibility == "true")
			{
				// we set style to block for IE
				// but for firefox we use table-row
				tr.style.display = canSee;
			}
			else if(bVisibility == "false")
			{
				tr.style.display = 'none';
			}
		}
	}
}

///////////////////////////////////////////////////////////////////////////////
// read a file from the server 
function checkValid(type, id, trName, tdName, root)
{ 	
	// only continue if xmlHttp isn't void
	str = encodeURIComponent( document.getElementById(id).value );
	if (str != ''){
		if (xmlHttp) 
		{ 
			// try to connect to the server 
			try { 
				
				_trName = trName;
				_tdName = tdName;			
				xmlHttp.open("GET", root + "check_signup.php?type="+type + "&" + type + "=" + str , true); 	
				xmlHttp.onreadystatechange = handleCheckValid; 					
				xmlHttp.send(null); 
			} 
		
			// display the error in case of failure 
			catch (e) 
			{ 			
				 //alert("Can't connect to server:\n" + e.toString()); 
			}			
		}
	}else {
		showTR(trName, "false");
	}
	
}

//kiểm tra dữ liệu,dùng cho franchise
function checkValidFranchise(type, id, trName, tdName, root, isFranchise)
{ 	
	// only continue if xmlHttp isn't void
	str = encodeURIComponent( document.getElementById(id).value );		
	if(str != '')
	{
		if (xmlHttp) 
		{ 
			// try to connect to the server 
			try { 
				
				_trName = trName;
				_tdName = tdName;
				xmlHttp.open("GET", root+"member/check_signup.php?type="+type+"&"+type+"=" + str + "&isfranchise="+ isFranchise , true); 			
				//xmlHttp.open("GET", "check_signup.php?type="+type+"&"+type+"=" + str , true); 				
				xmlHttp.onreadystatechange = handleCheckValid; 					
				xmlHttp.send(null); 
			} 
		
			// display the error in case of failure 
			catch (e) 
			{ 			
				 //alert("Can't connect to server:\n" + e.toString()); 
			}
			/*document.frmSignUp.action = root+"member/check_signup.php?type="+type+"&"+type+"=" + str ;
			document.frmSignUp.submit();*/
		}
	}
	else {
		showTR(trName, "false");
	}
}


// function called when the state of the HTTP request changes 
function handleCheckValid() 
{ 	
	// when readyState is 4, we are ready to read the server response 	
	if (xmlHttp.readyState == 4) 
	{ 	
		// continue only if HTTP status is "OK" 
		if (xmlHttp.status == 200) 	{ 
			try { 				
				// extract the XML retrieved from the server 
				xmlResponse = xmlHttp.responseXML;					
				// obtain the document element (the root element) of the XML structure 
				xmlDocumentElement = xmlResponse.documentElement;
				// get the text message, which is in the first child of 
				// the the document element 
				szMessage = xmlDocumentElement.getElementsByTagName("result").item(0).firstChild.data;
				szColor   = xmlDocumentElement.getElementsByTagName("color").item(0).firstChild.data;
				if (szMessage!= ''){
					document.getElementById(_tdName).innerHTML = '<font color="' + szColor + '">' + szMessage + '</font>';
				
					if(document.getElementById(_trName).style.display == "none")
					{
						showTR(_trName, "true");
					}
					xhrOk = true;
				}else{
					document.getElementById(_tdName).innerHTML = '<font color="' + szColor + '">' + szMessage + '</font>';
					if(document.getElementById(_trName).style.display != 'none')
					{
						showTR(_trName, "false");
					}
					xhrOk = true;	
				}
			} 
			catch(e) 
			{ 
				// display error message 
				//alert("Error reading the response: " + e.toString()); 
			} 
		} 		
	}	
}

///////////////////////////////////////////////////////////////////////////////
// read a file from the server 
function checkMemberInfo(root, id)
{ 	
	// only continue if xmlHttp isn't void	
	if (id != 'txtvehicle')
		str = encodeURIComponent( document.getElementById(id).value );	
	else
		str = encodeURIComponent( document.getElementById("txtvehicle1").value ) + "-" + encodeURIComponent( document.getElementById("txtvehicle2").value );
	if (str != ''){
		if (xmlHttp) 
		{ 
			// try to connect to the server 
			//try { 	
				xmlHttp.open("GET", root+"check_member_secondary_info.php?txtname=" + str, true); 			
				xmlHttp.onreadystatechange = handleCheckMemberInfo; 					
				xmlHttp.send(null); 
			//} 		
			// display the error in case of failure 
			//catch (e) 
			//{ 			
				 //alert("Can't connect to server:\n" + e.toString()); 
			//}
			/*document.frmSignUp.action = root+"member/check_signup.php?type="+type+"&"+type+"=" + str ;
			document.frmSignUp.submit();*/
		}
	}
}

// function called when the state of the HTTP request changes 
function handleCheckMemberInfo() 
{ 	

	// when readyState is 4, we are ready to read the server response 	
	if (xmlHttp.readyState == 4) 
	{ 	
		// continue only if HTTP status is "OK" 
		if (xmlHttp.status == 200) 	{ 
			//try { 				
				// extract the XML retrieved from the server 
				xmlResponse = xmlHttp.responseXML;
				// obtain the document element (the root element) of the XML structure 
				xmlDocumentElement = xmlResponse.documentElement;
				// get the text message, which is in the first child of 
				// the the document element 
				if (xmlDocumentElement.getElementsByTagName("title").item(0) != null)
				{
					szTitle = xmlDocumentElement.getElementsByTagName("title").item(0).firstChild.data;
					szTag   = xmlDocumentElement.getElementsByTagName("tag").item(0).firstChild.data;				
					document.getElementById("lbl").innerHTML = szTitle+ " :";
					document.getElementById("tdInput").innerHTML = szTag;
					//alert(szTitle +"  "+ szTag+ "       "+document.getElementById("tdInput").innerHTML);
				}
				else
				{	
					document.getElementById("lbl").innerHTML = "";
					document.getElementById("tdInput").innerHTML = "";
					
				}
				xhrOk = true;
			//} 
			//catch(e) 
			//{ 
				// display error message 
				//alert("Error reading the response: " + e.toString()); 
			//} 
		} 		
	}	
}
///////////////////////////////////////////////////////////////////////////////
// read a file from the server 
function changeCountry( strCountry, strLink, tabIndex )
{ 		
	// only continue if xmlHttp isn't void 
	if (xmlHttp) 
	{ 
		// try to connect to the server 
		try 
		{ 							
			xmlHttp.open("GET", strLink + "location.php?type=country&country=" + strCountry + '&signup=Y&tabindex=' + tabIndex, true); 			
			xmlHttp.onreadystatechange = handleChangeCountry; 			
			xmlHttp.send(null); 
		} 
		// display the error in case of failure 
		catch (e) 
		{ 			
			 alert("Can't connect to server:\n" + e.toString()); 
		}
	}	
}

// function called when the state of the HTTP request changes 
function handleChangeCountry() 
{ 
	// when readyState is 4, we are ready to read the server response 
	if (xmlHttp.readyState == 4) 
	{ 
		// continue only if HTTP status is "OK" 
		if (xmlHttp.status == 200) 	{ 
			try { 				
				// extract the XML retrieved from the server 
				xmlResponse = xmlHttp.responseXML;				
				// obtain the document element (the root element) of the XML structure 
				xmlDocumentElement = xmlResponse.documentElement;
				// get the text message, which is in the first child of 
				// the the document element 
				if (xmlDocumentElement.getElementsByTagName("province").item(0) != null)
				{
					szCity = xmlDocumentElement.getElementsByTagName("province").item(0).firstChild.data;				
					document.getElementById("idProvinceCode").innerHTML = szCity;
									
				}
			} 
			catch(e) 
			{ 
				// display error message 
				alert("Error reading the response: " + e.toString()); 
			} 
		} 
		else 
		{ 
			// display status message 
			alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
		}
	}
}
function checkValidRegister(type, id, trName, tdName, root)
{ 	
	// only continue if xmlHttp isn't void
	str = encodeURIComponent( document.getElementById(id).value );
	if (str != ''){
		if (xmlHttp) 
		{ 
			// try to connect to the server 
			try { 
				
				_trName = trName;
				_tdName = tdName;			
				xmlHttp.open("GET", root + "check_register.php?type="+type + "&" + type + "=" + str , true); 	
				xmlHttp.onreadystatechange = handleCheckValid; 					
				xmlHttp.send(null); 
			} 
		
			// display the error in case of failure 
			catch (e) 
			{ 			
				 //alert("Can't connect to server:\n" + e.toString()); 
			}			
		}
	}else {
		showTR(trName, "false");
	}
	
}

