var xmlHttp

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
  	{
		xmlHttp=new XMLHttpRequest();
  	}
	catch (e)
	{
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function checkusername()
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	
	var url="checkexisted.php?CheckValue="+document.FormName.Username.value+"&Types=Username&TName=member&sid="+Math.random();

	xmlHttp.onreadystatechange=UsernameStateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function UsernameStateChanged() 
{ 
	if (xmlHttp.readyState==4)
	{
		document.FormName.UsernameExisted.value=xmlHttp.responseText;
	}
}

function CheckAvailability(UserName)
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url="checkavailability.php?userid="+UserName+"";
	xmlHttp.onreadystatechange=CheckAvailStateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function CheckAvailStateChanged()
{
	if (xmlHttp.readyState==4)
	{
		var userstatus = document.getElementById("userstatus");
		document.getElementById('ajax_load').style.display = 'none';
		userstatus.innerHTML = xmlHttp.responseText;
	}
}

function admincheckusername()
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url="checkexistedadmin.php?CheckValue="+document.FormName.Username.value+"&ExcludeID="+document.FormName.ExcludeID.value+"&Types=Username&TName=member&sid="+Math.random();

	xmlHttp.onreadystatechange=AdminUsernameStateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function AdminUsernameStateChanged() 
{ 
	
	if (xmlHttp.readyState==4)
	{ 
		document.FormName.UsernameExisted.value=xmlHttp.responseText;
	}
}

function admincheckemail()
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url="checkexistedadmin.php?CheckValue="+document.FormName.Email.value+"&Types=Email&TName=member&sid="+Math.random();

	xmlHttp.onreadystatechange=AdminEmailStateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function AdminEmailStateChanged() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		document.FormName.EmailExisted.value=xmlHttp.responseText;
	}
}

function validateLogin()
{
	var errormsg;
	errormsg = "";

	if (document.Login.Username.value == "" || document.Login.Username.value == "Username")
		errormsg += "Please fill in 'Username'.\n";
		
	if (document.Login.Password.value == "")
		errormsg += "Please fill in 'Password'.\n";

	if ((errormsg == null) || (errormsg == ""))
	{
		if (document.Login.checker.checked) { toMem(this); }
		return true;
	}
	else
	{
		alert(errormsg);
		return false;
	}
}

function validateLoginBox()
{
	var errormsg;
	errormsg = "";

	if (document.LoginBox.Username.value == "")
		errormsg += "Please fill in 'Username'.\n";
		
	if (document.LoginBox.Password.value == "")
		errormsg += "Please fill in 'Password'.\n";

	if ((errormsg == null) || (errormsg == ""))
	{
		if (document.LoginBox.checker.checked) { toMemLoginBox(this); }
		return true;
	}
	else
	{
		alert(errormsg);
		return false;
	}
}

function validateLoginBoxPop()
{
	var errormsg;
	errormsg = "";

	if (document.LoginBoxPop.Username.value == "")
		errormsg += "Please fill in 'Username'.\n";
		
	if (document.LoginBoxPop.Password.value == "")
		errormsg += "Please fill in 'Password'.\n";

	if ((errormsg == null) || (errormsg == ""))
	{
		return true;
	}
	else
	{
		alert(errormsg);
		return false;
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
function newCookie(name,value,days) 
{
	var days = 1;
	if (days) {
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+date.toGMTString(); }
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/"; 
}

function readCookie(name)
{
	var nameSG = name + "=";
	var nuller = '';
	if (document.cookie.indexOf(nameSG) == -1)
		return nuller;
	
	var ca = document.cookie.split(';');
	for(var i=0; i<ca.length; i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameSG) == 0) return c.substring(nameSG.length,c.length); 
	}
	return null; 
}

function eraseCookie(name)
{
	newCookie(name,"",1);
}

function toMem(a)
{
	newCookie('username', document.Login.Username.value);
	newCookie('password', document.Login.Password.value);
}

function Checker(a)
{
	if(document.Login.Username.value != '' && document.Login.Password.value != '')  
	{  	
		document.Login.checker.checked=true;
	}
}

function delMem(a)
{
	if(document.Login.checker.checked==false)
	{
		eraseCookie('username');
		eraseCookie('password');
	}	
}

function toMemLoginBox(a)
{	
	newCookie('username', document.LoginBox.Username.value);
	newCookie('password', document.LoginBox.Password.value);
}

function CheckerLoginBox(a)
{
	if(document.LoginBox.Username.value != '' && document.LoginBox.Password.value != '')  
	{  	
		document.LoginBox.checker.checked=true;
	}
}

function delMemLoginBox(a)
{
	if(document.LoginBox.checker.checked==false)
	{
		eraseCookie('username');
		eraseCookie('password');
	}	
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
function validateQuickSearch()
{
	var errormsg;
	errormsg = "";
	
	if (document.QuickSearch.txtQuickSearch.value == "" || document.QuickSearch.txtQuickSearch.value == "Enter Keywords Here")
		errormsg += "Please fill in 'Quick Search'.\n";

	if ((errormsg == null) || (errormsg == ""))
	{
		return true;
	}
	else
	{
		alert(errormsg);
		return false;
	}
}

function NewsTextDisappear(formname, fieldname, fieldvalue)
{
	if (eval("document."+formname+"."+fieldname+".value") == fieldvalue)
	{
		eval("document."+formname+"."+fieldname+".value = ''");
	}
	else if (eval("document."+formname+"."+fieldname+".value") == '')
	{
		eval("document."+formname+"."+fieldname+".value = '"+fieldvalue+"'");
	}
}

/*Password Text Begin*/
function PasswordChangeBox() 
{
  document.getElementById('PasswordDiv1').style.display='none';
  document.getElementById('PasswordDiv2').style.display='';
  document.getElementById('Password').focus();
}
function PasswordRestoreBox() 
{
	if(document.getElementById('Password').value=='')
	{
	  document.getElementById('PasswordDiv1').style.display='';
	  document.getElementById('PasswordDiv2').style.display='none';
	}
}
/*Password Text End*/

function MyPopUpWin(pic, picWidth, picHeight) 
{
	var iMyWidth;
	var iMyHeight;
	if(parseFloat(window.screen.width) > parseFloat(picWidth))
		iMyWidth = (parseFloat(window.screen.width) - parseFloat(picWidth))/2;
	else
		iMyWidth = 0;
	if(parseFloat(window.screen.height) > parseFloat(picHeight))
		iMyHeight = (parseFloat(window.screen.height) - parseFloat(picHeight))/2;
	else
		iMyHeight = 0;
	var win2 = window.open(pic,"Window2","height=" + (parseFloat(picHeight) + 20) + ",width=" + (parseFloat(picWidth) + 10) + ",left=" + iMyWidth + ",top=" + iMyHeight + ",status=no,resizable=no,toolbar=no,menubar=no,scrollbars=no,location=no,directories=no");
	win2.focus();
}

function AjaxOpenAnotherWindow(title, url)
{
	var p = parent; 
	p.$("#TB_window").remove(); 
	p.$("body").append("<div id='TB_window'></div>"); 
	p.tb_show(title, url, ""); 
}

function validateRegisterForm()
{
	var errormsg;
	errormsg = "";
	
	if (document.FormName.Username.value == "")
		errormsg += "Please fill in 'Username'.\n";
	else
	{
		if (document.FormName.Username.value.length < 6)
			errormsg += "Please fill in 6 digits or more in 'Username'.\n";
			
		if (document.FormName.UsernameExisted.value == "Yes")
			errormsg += "The username is currently in used. Please fill in another 'Username'.\n";
	}
		
	if (document.FormName.NewPassword.value == "")
		errormsg += "Please fill in 'New Password'.\n";
	else
	{
		if (document.FormName.NewPassword.value.length < 7)
			errormsg += "Please fill in 7 digits or more in 'New Password'.\n";
	}
		
	if (document.FormName.RetypePassword.value == "")
		errormsg += "Please fill in 'Re-type New Password'.\n";
	else
	{
		if (document.FormName.RetypePassword.value.length < 7)
			errormsg += "Please fill in 7 digits or more in 'Re-type New Password'.\n";
	}
	
	if (document.FormName.NewPassword.value != "" || document.FormName.RetypePassword.value != "")
	{
		if (document.FormName.NewPassword.value != document.FormName.RetypePassword.value)
			errormsg += "Please ensure 'New Password' and 'Re-type New Password' are the same.\n";
	}
	
	if (document.FormName.FullName.value == "")
		errormsg += "Please fill in 'Full Name'.\n";
		
	/*if (document.FormName.Designation.value == "")
		errormsg += "Please fill in 'Designation'.\n";*/
		
	if (document.FormName.ContactTel.value == "")
		errormsg += "Please fill in 'Contact Tel'.\n";
		
	if (document.FormName.ContactEmail.value == "")
		errormsg += "Please fill in 'Contact Email'.\n";
	else
	{
		if (!isEmail(document.FormName.ContactEmail.value))
			errormsg += "Please fill in valid email address in 'Contact Email'.\n";		
	}	
	
	if (document.FormName.CompanyName.value == "")
		errormsg += "Please fill in 'Company Name'.\n";
		
	if (document.FormName.TypeOfBusiness.value == "")
		errormsg += "Please select 'Type Of Business'.\n";
		
	if (document.FormName.Tel.value == "")
		errormsg += "Please fill in 'Tel'.\n";
				
	/*if (document.FormName.Fax.value == "")
		errormsg += "Please fill in 'Fax'.\n";
		
	if (document.FormName.Website.value == "")
		errormsg += "Please fill in 'Website'.\n";*/
		
	if (document.FormName.Address.value == "")
		errormsg += "Please fill in 'Address'.\n";
		
	if (document.FormName.Country.value == "")
		errormsg += "Please select in 'Country'.\n";
		
	/*if (document.FormName.State.value == "")
		errormsg += "Please fill in 'State'.\n";
		
	if (document.FormName.City.value == "")
		errormsg += "Please fill in 'City'.\n";*/
		
	if (document.FormName.PostalCode.value == "")
		errormsg += "Please fill in 'Postal Code'.\n";
	
	if (document.FormName.security_code.value == "")
		errormsg += "Please fill in 'Verification Code'.\n";
	else
	{
		if (document.FormName.security_code.value != document.FormName.codeans.value)
			errormsg += "Please fill in valid 'Verification Code'.\n";
	}
	
	if ((errormsg == null) || (errormsg == ""))
	{
		document.FormName.btnSubmit.disabled=true;
		return true;
	}
	else
	{
		alert(errormsg);
		return false;
	}
}
