/*
Description: Validation on the phone
Parameters: String of the phone
Date: 24 October 2007
*/
function ValidatePhone(str)
{
    bValid = true;
	for (i=0; i<str.length; i++){
      chr = str.charAt(i);      
      if (!((chr >= '0' && chr <= '9')||(chr==' ')||(chr=='-'))){
        bValid = false;
        break;
      }
	}

	if (str.length<8){
		bValid=false;
	}
	return bValid;
}

/*
Description: Validation on the email address
Parameters: String of the email address
Date: 4 February 2002
*/
function ValidateEmail(str)
{
	bValid = false;
	if (str)
	{
		i = str.indexOf("@");
		if (i > 0)
		{
			j = str.indexOf(".", i);    
			if (j > 0)
			{
			  if((str.indexOf("/")<0) && (str.indexOf(" ")<0))
					if (str.charAt(str.length-1) != ".")
						bValid = true;
			}
		}
	}
	else
		bValid = true;
     
	if (!(str.indexOf("'")<0))	 
	{
		bValid = false;
	}
		
	if (!bValid)
	{ 
		return(false);
	}
	return(true);
}

/*
Description: Validation on the user name, user name can only allow character and numeric character, with lowercase
Parameters: String of the user name
Date: 4 February 2002
*/
function ValidateUsername(str)
{
  bValid = true;
  if (str)
  {
    for (i=0; i<str.length; i++)
    {
      chr = str.charAt(i);      
      if (!(chr >= 'a' && chr <= 'z' || chr >= '0' && chr <= '9'))       
      {
        bValid = false;
        break;
      }
    }
  }
  
  if (!bValid)
       { 
      return(false);
     }  
return(true);
}

/*
Description: Validation on the first or last name
Parameters: String of the first or last name
Date: 4 February 2002
*/
function ValidateFirstname(str)
{
  bValid = true;
  if (str)
  {
    for (i=0; i<str.length; i++)
    {
      chr = str.charAt(i);      
      if (!(chr == ' ' || chr >= 'A' && chr <= 'Z' || chr >= 'a' && chr <= 'z' || chr >= '0' && chr <= '9'))       
      {
        bValid = false;
        break;
      }
    }
  }
  
  if (!bValid)
       { 
      return(false);
     }  
return(true);
}

/*
Description: Validation on the password
Parameters: String of the password
Date: 4 February 2002
Task 1291, 18/8/2008, Sammy So, Ver.2
*/

// return asc code
function Asc(String)
{

	return String.charCodeAt(0);

}

function ValidatePassword(str)
{
	var ascCode = "";
	var flag = true
	var i = 0;
	
	if(str != "")
	{
		for(i=0;i<=str.length-1;i++)
		{
			ascCode = Asc(str.charAt(i));
			if(!(ascCode >= 32 && ascCode <= 126 && ascCode != 39))
			{
				flag = false
			}
		}
	}
	else
	{
		flag = false;
	}
	
	return flag;
/*
  bValid = true;
  if (str)
  {
    for (i=0; i<str.length; i++)
    {
      chr = str.charAt(i);
      if (chr == "'" || chr == '"')
      {
        bValid = false;
        break;
      }
    }
  }
  
  if (!bValid){
    return(false);
  }
  return true;
 */
}

/*
Description: Validation on the multi-select box
Parameters: Form Object, Number
Date: 4 February 2002
*/
function restrictUpto(selectobj, num)
{
	var totalSelected=0;
	for (i=0; i<selectobj.length; i++)
	{
		if (selectobj.options[i].selected == true)
		{  
		totalSelected++;
		if (totalSelected > num) /* clear all select element upper the limit */
			selectobj.options[i].selected = false;
		}
	}
	if (totalSelected > num)
	{
		return false;
	}
	else
		return true;
}

/*
Description: Validation on the company block
Parameters: Form Object, field name
Date: 20 February 2002
*/
function blockCntCheck(selectobj)
{
	var companyCnt = 0;
	for (i=0; i<selectobj.length; i++)
	{
		if (selectobj[i].checked == true)
		{
			companyCnt = companyCnt + 1;
		}
	}
	if (companyCnt < 11)
	{
		return true;
	}
	else
	{
		return false;
	}
}




function makeRequest(url,err_msg) {
var http_request = false;

if (window.XMLHttpRequest) { // Mozilla, Safari,...
  http_request = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
  try {
	http_request = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
	try {
	  http_request = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) {}
  }
}

if (!http_request) {
  //alert('Giving up :( Cannot create an XMLHTTP instance');
  return false;
}

http_request.onreadystatechange = function() { 
								  alertContents(http_request,err_msg); };
http_request.open('GET', url , true);
http_request.send(null);

}

function alertContents(http_request,err_msg) {
if (http_request.readyState == 4) {
  if (http_request.status == 200) {
	var result = http_request.responseText;

	//document.write(result);
	document.getElementById(err_msg).innerHTML = result;
  } else {
	//alert('There was a problem with the request.');
  }
}
}

/*
Description: Validation on the NickName
Parameters: String of the NickName
*/
function ValidateNickname(str)
{
	if (str.length>20){
		return false;
	}
	return true;
}
