function GetXmlHttpObject()
{
  var xmlHttp=null;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
  return xmlHttp;
} 
function stateChanged() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		//var ret=xmlHttp.responseText;
		location.href='index.php';
	}
}
function ValidateLoginForm(theform)
{

/*	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
	*/  
	if(!theform.user.value || !theform.pwd.value || !theform.security_code.value){
		alert('Please key in username, password and security code !');
		return false;
	}else{
	//	is_login(theform.user.value,theform.pwd.value)
		return true;
	}
}

function ValidateResetPwd(theform)
{

  
	if(!theform.ic.value || !theform.name.value){
		alert('Please key in your new IC and name!');
		return false;
	}else{
		return true;
	}
}

function ValidateChgPwd(theform)
{
	var msg="";

	if(!theform.oldpwd.value)msg+="Old password required. \n";

	if(theform.newpwd.value.length < 6)msg+="Please specify the password, minimum 6 characters. \n";
	else if(theform.newpwd.value != theform.conpwd.value)msg+="The confirm password does not match. \n";

	 if(msg){
		alert(msg)
		return false;
	}else return true;
	

}

function ValidateAddUser(theform){
	var msg="";


	if(!theform.username.value)msg+="Please specify the username. \n";
	else if(theform.username.value.length > 10)msg+="Username maximum 10 character. \n"; 
	if(!theform.name.value)msg+="Please key in the name. \n";
	if(!theform.level.value)msg+="Please select the position. \n";
//	if(!theform.access.value)msg+="Please select the access. \n";	
	if(theform.pwd.value.length < 6)msg+="Please specify the password, minimum 6 characters. \n";
	else if(theform.pwd.value != theform.cpwd.value)msg+="The confirm password does not match the first password. \n";


   if(msg){
		alert(msg)
		return false;
	}else return true;
}

function ValidateEditUser(theform){
	var msg="";

//	if(!theform.level.value)msg+="Please select the position. \n";
//	if(!theform.access.value)msg+="Please select the access. \n";	

	
	if(theform.pwd.value && theform.pwd.value.length < 6)msg+="Please specify the password, minimum 6 characters. \n";
	else if(theform.pwd.value != theform.cpwd.value)msg+="The confirm password does not match the first password. \n";

   if(msg){
		alert(msg)
		return false;
	}else return true;
}
function ValidateUpdateProfile(theform){
	var msg="";

	if(!theform.addr1.value)msg +="Please specify the Address. \n";			
	if(!theform.state.value)msg +="Please specify the state. \n";							
	if(!theform.postal.value)msg +="Please specify the postal. \n";			
	if(!theform.city.value)msg +="Please specify the city. \n";				
	if(!theform.country.value)msg +="Please specify the country. \n";					
	
	if(!theform.handphone.value && !theform.phone.value && !theform.directline.value)msg += "Please specify contact number. \n";				
	
	if((ValidateNumber(theform.handphone)==false) && theform.handphone.value)msg += "It was not specified handphone (numbers only). \n";				
	if((ValidateNumber(theform.phone)==false) && theform.phone.value)msg += "It was not specified office no (numbers only). \n";				
	if((ValidateNumber(theform.directline)==false) && theform.directline.value)msg += "It was not specified direct line no (numbers only). \n";				
	

   if(msg){
		alert(msg)
		return false;
	}else return true;
}


//  date format
/**
 * DHTML date validation script for dd/mm/yyyy. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
//		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
//		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
//		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
//		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
//		alert("Please enter a valid date")
		return false
	}
return true
}

//----------------------


function ValidateNumber(theinput)
{
   s=theinput.value
   if(s=="") return false;
   if(s.search)
   {
     return (s.search(new RegExp("^[- 0-9]+$","gi"))>=0)
   }
}
function ValidatePhoneNumber(theinput)
{
   s=theinput.value
   if(s=="") return false;
   if(s.search)
   {
     return (s.search(new RegExp("^[0-9]+$","gi"))>=0)
   }
}
function ValidateEmail(theinput)
{
   s=theinput.value
   if(s.search)
   {
      return (s.search(new RegExp("^([a-z0-9_]|\-|\\\.)+@(([a-z0-9_]|\-)+\\\.)+[a-z]{2,4}$","gi"))>=0)
   }
   if(s.indexOf)
   {
      at_character=s.indexOf('@')
      if(at_character<=0 || at_character+4>s.length)
         return false
   }
   if(s.length<6)
      return false
   else
      return true
}
function ValidatePrice(theinput)
{
   s=theinput.value
   if(s=="") return false;
   if(s.search)
   {
      return (s.search(new RegExp("^[0-9.]+$","gi"))>=0)
   }
}
function ValidateOnlyText(theinput)
{

   s=theinput.value
    if(s.search)
   {
      return (s.search(new RegExp("^[-@._' a-z ]+$","gi"))>=0)
   }
}
function ValidateText(theinput)
{
	str=theinput.value;	
	if(str == "") return false
	else if(ValidateOnlyText(theinput)==false)return false
	else return true
}

function ValidateReferral(theform){
	var msg="";

   if(msg){
		alert(msg)
		return false;
	}else return true;
}


function DialogWindow(theURL,winName,type) { 
	var features;
	if(type=="Classic")features ="width=650,height=600";
	else features=type;
	
  window.open(theURL,winName,features);
}
//-------------------------------
function getMemberList()
{
	var sMemberID=document.form.sType.value;
	var sName = document.form.sReferral.value;
	var fld = document.form.fld.value;
	var inpfld = document.form.inpfld.value;
	
	xmlHttp=GetXmlHttpObject();
	var url="ajax_memberlist.php";
	url=url+"?sMemberID="+sMemberID+"&sName="+sName+"&fld="+fld+"&inpfld="+inpfld;

	xmlHttp.onreadystatechange=stateChangedMember;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	

}
function stateChangedMember() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		document.getElementById("divResult").innerHTML=xmlHttp.responseText;
	}
}
// -----------------------------------------------
function postMemberRecord(no,nm,fd,inpfld)
{
	if(fd=="sid")window.opener.document.form.sid.value = no;
	else if(fd=="did")window.opener.document.form.did.value = no;
	
	if(inpfld=="name")window.opener.document.form.name.value = nm;
	else if(inpfld=="dname")window.opener.document.form.dname.value = nm;

	window.close();
}

// ----------------display name info
function stateChangedName() 
{ 
	var ret;
	if (xmlHttp.readyState==4)
	{ 
		ret=xmlHttp.responseText;
		var val=ret.split("|");

//	 var a=val[1].length;
//alert(a)
		if(val[1]=="dName ")document.form.dName.value = val[0];
		else if(val[1]=="dName2 ")document.form.dName2.value = val[0];
	}
}

function disName(val,inpfld)
{
	xmlHttp=GetXmlHttpObject();
	var url="ajax_disName.php?no="+val+"&inpfld="+inpfld;

	xmlHttp.onreadystatechange=stateChangedName;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	

}

//----------------------------



function randomPassword(length)
{
  chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  pass = "";
  for(x=0;x<length;x++)
  {
    i = Math.floor(Math.random() * 62);
    pass += chars.charAt(i);
  }
  return pass;
}
function formSubmit()
{
  document.form.pwd.value = randomPassword(8);
  return false;
}


function postCompany(no,nm)
{
	window.opener.document.form.sid.value = no;
	window.opener.document.form.person.value = nm;
	window.close();
}
function postPerson(no,nm)
{
	window.opener.document.form.sid.value = no;
	window.opener.document.form.company.value = nm;
	window.close();
}
function postCustomer(no,nm,type)
{
	window.opener.document.form.sid.value = no;
	window.opener.document.form.name.value = nm;
	window.opener.document.form.type.value = type;
	window.close();
}

function newvalue(type,val){
	if(val=="add"){
		var url="popup/popNewValue.php?type="+type;
		DialogWindow(url,'NewValue','width=300,height=150')
	}
}
function ValidateAddNewValue(theform){
	var msg="";

	if(!theform.type.value)msg +="Please select the type. \n";			
	if(!theform.name.value)msg +="Please specify the name. \n";			

  if(msg){
		alert(msg)
		return false;
	}else return true;
}
function ValidateAddNewPerson(theform){
	var msg="";

	if(!theform.name.value)msg +="Please specify the name. \n";			
	if(theform.race.value==1)msg +="Please select the race. \n";			
	if(!theform.gender.value)msg +="Please select the gender. \n";				
	if(theform.type.value==1)msg +="Please select the type. \n";				
	
	if(theform.phone1.value && ValidatePhoneNumber(theform.phone1)==false)msg += "It was not specified phone 1 (numbers only). \n";						
	else if(theform.phone1.value && !theform.phone1_label.value)msg += "Please select phone 1 label. \n";						

	if(theform.phone2.value && ValidatePhoneNumber(theform.phone2)==false)msg += "It was not specified phone 2 (numbers only). \n";						
	else if(theform.phone2.value && !theform.phone2_label.value)msg += "Please select phone 2 label. \n";						

	if(theform.phone3.value && ValidatePhoneNumber(theform.phone3)==false)msg += "It was not specified phone 3 (numbers only). \n";						
	else if(theform.phone3.value && !theform.phone3_label.value)msg += "Please select phone 3 label. \n";						

	if(theform.fax.value && ValidatePhoneNumber(theform.fax)==false)msg += "It was not specified fax (numbers only). \n";						
	if(msg){
		alert(msg)
		return false;
	}else return true;
}
function clearFields(){
	document.form.reset();
}
function pintomenu(fld){
	var sType=document.form.sType.value;
	var sReferral=document.form.sReferral.value;
	var role=document.form.role.value;
	var type=document.form.type.value;
	var industry=document.form.industry.value;
	var country=document.form.country.value;
	
	var url="popup/pinMenu.php?fld="+fld+"&sType="+sType+"&sReferral="+sReferral+"&role="+role+"&type="+type+"&industry="+industry+"&country="+country;
	DialogWindow(url,'PintoMenu','width=300,height=150')

}
function pintomenu_company(fld){
	var sType=document.form.sType.value;
	var sReferral=document.form.sReferral.value;
	var type=document.form.type.value;
	var industry=document.form.industry.value;
	var classification=document.form.classification.value;
    var country=document.form.country.value;

	var url="popup/pinMenuComp.php?fld="+fld+"&sType="+sType+"&sReferral="+sReferral+"&type="+type+"&industry="+industry+"&country="+country+"&classification="+classification;
	DialogWindow(url,'PintoMenu','width=300,height=150')

}
function removepinmenu(id){
	var ret=confirm('Are you sure remove pin from menu?');	
	if(ret==true){
		xmlHttp=GetXmlHttpObject()		
		var url="ajax_removepin.php?id="+id;

		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}else return false;

}
function ValidateAddCompany(theform){
	var msg="";

	if(!theform.name.value)msg +="Please specify the company name. \n";		
	if(theform.type.value==1)msg +="Please select the type. \n";					
	
	if(theform.phone1.value && ValidatePhoneNumber(theform.phone1)==false)msg += "It was not specified phone 1 (numbers only). \n";						
	else if(theform.phone1.value && !theform.phone1_label.value)msg += "Please select phone 1 label. \n";						

	if(theform.phone2.value && ValidatePhoneNumber(theform.phone2)==false)msg += "It was not specified phone 2 (numbers only). \n";						
	else if(theform.phone2.value && !theform.phone2_label.value)msg += "Please select phone 2 label. \n";						

	if(theform.phone3.value && ValidatePhoneNumber(theform.phone3)==false)msg += "It was not specified phone 3 (numbers only). \n";						
	else if(theform.phone3.value && !theform.phone3_label.value)msg += "Please select phone 3 label. \n";						

	if(theform.fax.value && ValidatePhoneNumber(theform.fax)==false)msg += "It was not specified fax (numbers only). \n";						
	if(msg){
		alert(msg)
		return false;
	}else return true;
}
function ValidateAddMemo(theform){
	var msg="";

	if(!theform.group.value)msg +="Please select group recipient. \n";		
	if(!theform.subject.value)msg +="Please key in the subject. \n";					
	if(msg){
		alert(msg)
		return false;
	}else return true;
}
function ValidateAddEB(theform){
	var msg="";

	if(!theform.name.value)msg +="Please key in template name. \n";		
	
	if(msg){
		alert(msg)
		return false;
	}else return true;
}
function show_Table(varclass,str) {
    var myclass=varclass
	divColl = document.all.tags("table");	


    for (i=0; i<divColl.length; i++) {
    	if (divColl(i).className == myclass) {
            divColl(i).style.display =str;
        }			
    }
}
function enable_person_alert(str){
	if(str=="Individual"){
		show_Table('person','')
	}else show_Table('person','none')
}
function enable_company_alert(str){
	if(str=="Individual"){
		show_Table('company','')
	}else show_Table('company','none')
}
function sendtype(str){

	if(str=="Date"){
		document.form.year.disabled = false;
		document.form.month.disabled = false;
		document.form.day.disabled = false;
	}else{
		document.form.year.disabled = true;
		document.form.month.disabled = true;
		document.form.day.disabled = true;
	}
}
function ValidateAddAlert(theform){
	var msg="";

//	if(!theform.name.value)msg +="Please key in alert name. \n";		
	if(!theform.template.value)msg +="Please choose the template. \n";			
	if(!theform.from.value)msg +="Please key in from. \n";			
	if(!theform.subject.value)msg +="Please key in subject name. \n";		
	if(msg){
		alert(msg)
		return false;
	}else return true;
}

function ValidateAddResponder(theform){
	var msg="";

	if(!theform.title.value)msg +="Please key in the title. \n";		
	if(!theform.detail.value)msg +="Please key in the details. \n";			
	if(!theform.status.value)msg +="Please choose the status. \n";		
	if(msg){
		alert(msg)
		return false;
	}else return true;
}
function ValidateAddCase(theform){
	var msg="";

	if(!theform.name.value)msg +="Please select the customer name. \n";		
	if(!theform.title.value)msg +="Please key in the title. \n";		
	if(!theform.detail.value)msg +="Please key in the details. \n";			
	if(!theform.status.value)msg +="Please choose the status. \n";		
	if(msg){
		alert(msg)
		return false;
	}else return true;
}
function ValidateEditCase(theform){
	var msg="";

	if(!theform.detail.value)msg +="Please key in the details. \n";			
	if(!theform.status.value)msg +="Please choose the status. \n";		
	if(msg){
		alert(msg)
		return false;
	}else return true;
}
function ValidateAddSMS(theform){
	var msg="";

	if(!theform.name.value)msg +="Please key in the template name. \n";		
	if(!theform.message.value)msg +="Please type in the message. \n";			

	if(msg){
		alert(msg)
		return false;
	}else return true;
}
function ValidateAddSmsAlert(theform){
	var msg="";

	if(!theform.sender.value)msg +="Please key in senderID. \n";		
//	if(!theform.name.value)msg +="Please key in alert name. \n";		
	if(!theform.template.value)msg +="Please choose the template. \n";			

	if(msg){
		alert(msg)
		return false;
	}else return true;
}

function ValidateProfile(theform){
	var msg="";


	if(!theform.username.value)msg+="Please specify the username. \n";
	else if(theform.username.value.length > 10)msg+="Username maximum 10 character. \n"; 
	if(!theform.name.value)msg+="Please key in the name. \n";

   if(msg){
		alert(msg)
		return false;
	}else return true;
}
function postEmail(fld,nm)
{

	if(fld=="cc"){
		if(window.opener.document.form.cc.value)window.opener.document.form.cc.value = window.opener.document.form.cc.value + ","+nm;
		else window.opener.document.form.cc.value = nm;
	}else if(fld=="bcc"){
		if(window.opener.document.form.bcc.value)window.opener.document.form.bcc.value = window.opener.document.form.bcc.value + ","+nm;
		else window.opener.document.form.bcc.value = nm;
	}

	window.close();
}