function isEmail(src)
{
	var pattern1  = /^\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w+$/;
	if (!pattern1.test(src))
	{
		return false;
	}

	var pattern2 = /^.*@[^_]*$/;
	if (!pattern2.test(src))
	{
		return false;
	}

	return true;
}

function isEmails(src)
{
var pattern1  = /^(\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w+),?(\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w+)?$/;
	if (!pattern1.test(src))
	{
		return false;
	}

	var pattern2 = /^(.*@[^_]*),?(.*@[^_]*)?$/;
	if (!pattern2.test(src))
	{
		return false;
	}

	return true;
}

function string_isInt(ObjValue){						    /* check the string is Int : 0-9*/
	if (ObjValue.length > 0)
	{
	   var pattern = /^[0-9]*$/;
	   if (!pattern.test(ObjValue)) {
	   		return false;
	   }
	}
	return true;
}

function isModel(src)
{
  var pattern1  = /^[0-9a-zA-Z|-]*$/;
	if (!pattern1.test(src) || src == "")
	{
		return false;
	}

	return true;
}

function isSn(src)
{ 
	var pattern = /^[0-9A-Z]*$/;
	if(!pattern.test(src) || src == "")
	{
		return false;
	}
	
	return true;
}

function check_sn(sn1_val,sn2_val,sn3_val,sn4_val)
{
	//sn
	var data1 = parseInt(sn1_val+sn2_val,10)-1;//alert(data1);
	var data2 = new Array(4);
	var data3;
	var data_36_last4; 
	var data_36_last5;
	for (i=0;i<4;i++)
	{
		data2[i] = sn4_val.substring(i,i+1);
		if(data2[i] >= 'A')
		{
		  data2[i] = sn4_val.charCodeAt(i)-65+10;
		}	
		data2[i] = parseInt(data2[i],10);
		//alert(data2[i]);	
	}
	data_36_last4 = data2[3]+data2[2]*36+data2[1]*(36*36)+data2[0]*(36*36*36);
	//alert(data_36_last4);  
	
	data3 = sn3_val.substring(3,4);
	if(data3 >= 'A')
	{
		data3 = sn3_val.charCodeAt(i)-65+10;
	}
	data3 = parseInt(data3,10);
	data_36_last5 = data_36_last4+data3*(36*36*36*36);
	
  if(data1 != data_36_last4 && data1 != data_36_last5)
  {
  	return false;
  }
  else
  {
  	return true;
  }
}

function check_sn_model(sn,model)
{
	if((sn.search("H0")!= -1 && model == "office")
	   || (sn.search("H2")!= -1 && model == "H500")
	   || (sn.search("H3")!= -1 && model == "H600")
	   || (sn.search("P0")!=-1 && model == "P100")
	   || (sn.search("P2")!=-1 && model == "P200")
	   || (sn.search("P3")!= -1 && model == "P200")
	   || (sn.search("A")!=-1 && model == "Enterprise") 
	   || (sn.search("B")!=-1 && model == "B1000")
	   )
	{
		return true;
	}
	else
	{
		return false;
	}
}

/*regist info on membership page*/
function save_submit()
{
	var full_name = document.getElementById("full_name");
	var email = document.getElementById("email");
	var mobile = document.getElementById("mobile");
	var country = document.getElementById("country");
	var usr_name = document.getElementById("usr_name");
	var usr_pwd = document.getElementById("usr_pwd");
	var usr_pwd_check = document.getElementById("usr_pwd_check");
	var snp1 = document.getElementById("snp1");
	var snp2 = document.getElementById("snp2");
	var snp3 = document.getElementById("snp3");
	var snp4 = document.getElementById("snp4");
	
	var snb1 = document.getElementById("snb1");
	var snb2 = document.getElementById("snb2");
	var snb3 = document.getElementById("snb3");
	var snb4 = document.getElementById("snb4");
	
	var snh1 = document.getElementById("snh1");
	var snh2 = document.getElementById("snh2");
	var snh3 = document.getElementById("snh3");
	var snh4 = document.getElementById("snh4");
	
	var sn41 = document.getElementById("sn41");
	var sn42 = document.getElementById("sn42");
	var sn43 = document.getElementById("sn43");
	var sn44 = document.getElementById("sn44");
	
	var sn51 = document.getElementById("sn51");
	var sn52 = document.getElementById("sn52");
	var sn53 = document.getElementById("sn53");
	var sn54 = document.getElementById("sn54");
	
	var model1 = document.getElementById("model1");
	var model2 = document.getElementById("model2");
	var model3 = document.getElementById("model3");
	var model4 = document.getElementById("model4");
	var model5 = document.getElementById("model5");
	
	var term_checked = document.getElementById("term_checked"); 
	var verify_code = document.getElementById("verify_code");
	var randval = document.getElementById("randval");
	
	var pattern_lsc = /^(\w{4})$/;
	var pattern_mobile = /^([0-9]|-)*$/;
	
	if(full_name.value=="")
	{
		alert("Please input your Full Name.");
		return false;
	}
	
	if(email.value=="")
	{
		alert("Please input Email Address.");
		return false;
	}
	else
	{
		if (isEmails(email.value) == false)
	  {
		alert("Email Address format error.");
		return false;
	  }
	}
	
	if(mobile.value=="")
	{
		alert("Please input Phone Number(Mobile).");
		return false;
	}
	else
	{
		if(!pattern_mobile.test(mobile.value))
		{
			alert("Phone Number(Mobile) format error");
			return false;
		}
	}
	
	if(country.value == "")
	{
		alert("Please input Country");
		return false;
	}
	
	if(usr_name.value == "")
	{
		alert("Please input User Name");
		return false;
	}
	
	if(usr_pwd.value == "")
	{
		alert("Please input Password");
		return false;
	}
	
	if(usr_pwd_check.value == "")
	{
		alert("Please input Confirm Password");
		return false;
	}
	
	if(usr_pwd.value != usr_pwd_check.value)
	{
		alert("Password and Confirm Password don't match");
		return false;
	}
	
	if(snp1.value == "" || snp2.value == "" || snp3.value == "" || snp4.value == "")
	{
		alert("Please input IPX Serial Number.");
		return false;
	}
	else
  {
    if(!check_sn(snp1.value,snp2.value,snp3.value,snp4.value))
    {
	    alert("IPX Serial Number format error,Serial Number can be found on the back of IPX product.");
	    return false;
    }
  }
  
  if(!check_sn_model(snp3.value,model1.value))
  {
  	alert("IPX Serial Number doesn't match with model");
		return false;
  }
  
  /*check sn2*/
  if(snb1.value != "" || snb2.value != "" || snb3.value != "" || snb4.value != "")
	{
		if(!check_sn(snb1.value,snb2.value,snb3.value,snb4.value))
    {
	    alert("IPX Serial Number format error,Serial Number can be found on the back of IPX product.");
	    return false;
    }
    
    if(!check_sn_model(snb3.value,model2.value))
    {
  	  alert("IPX Serial Number doesn't match with model");
		  return false;
    }
	}
  /*end check sn2*/
  
  /*check sn3*/
  if(snh1.value != "" || snh2.value != "" || snh3.value != "" || snh4.value != "")
	{
		if(!check_sn(snh1.value,snh2.value,snh3.value,snh4.value))
    {
	    alert("IPX Serial Number format error,Serial Number can be found on the back of IPX product.");
	    return false;
    }
    
    if(!check_sn_model(snh3.value,model3.value))
    {
  	  alert("IPX Serial Number doesn't match with model");
		  return false;
    }
	}
  /*end check sn3*/
  
  /*check sn4*/
  if(sn41.value != "" || sn42.value != "" || sn43.value != "" || sn44.value != "")
	{
		if(!check_sn(sn41.value,sn42.value,sn43.value,sn44.value))
    {
	    alert("IPX Serial Number format error,Serial Number can be found on the back of IPX product.");
	    return false;
    }
    
    if(!check_sn_model(sn43.value,model4.value))
    {
  	  alert("IPX Serial Number doesn't match with model");
		  return false;
    }
	}
  /*end check sn4*/
  
  /*check sn5*/
  if(sn51.value != "" || sn52.value != "" || sn53.value != "" || sn54.value != "")
	{
		if(!check_sn(sn51.value,sn52.value,sn53.value,sn54.value))
    {
	    alert("IPX Serial Number format error,Serial Number can be found on the back of IPX product.");
	    return false;
    }
    
    if(!check_sn_model(sn53.value,model5.value))
    {
  	  alert("IPX Serial Number doesn't match with model");
		  return false;
    }
	}
  /*end check sn5*/
    
  if(verify_code.value == "")
  {
  	alert("Please input verification code.");
  	return false;
  }
  
  if(verify_code.value != randval.value)
	{
		alert("Verfication Code error.");
/*		var form_obj1 = document.getElementById("form1");
	  form_obj1.action = "membership.php?cmd=unclear";
	  form_obj1.submit();*/
	  return false;
	}
	
	if(term_checked.checked == false)
  {
  	alert("Please read the terms first.");
  	return false;
  }
  
  return true;
}

function update_vercode()
{
	var form_obj1 = document.getElementById("form1");
	form_obj1.action = "membership.php?cmd=unclear";
	form_obj1.submit();
	return;
}

function update_vercode_did()
{
	var form_obj1 = document.getElementById("form1");
	form_obj1.action = "request-did.php?cmd=unclear";
	form_obj1.submit();
	return;
}

function update_vercode_account()
{
	var form_obj1 = document.getElementById("form1");
	form_obj1.action = "myaccount.php?cmd=unclear";
	form_obj1.submit();
	return;
}


//for sn:输入完后自动跳到下一文本框
function snp1_onkeyup() 
{
 document.getElementById('snp1').value=document.getElementById('snp1').value.toUpperCase(); 
 
 if(document.form1.snp1.value.length==4)
 {
  document.form1.snp2.focus();
 }
} 
function snp2_onkeyup() 
{
 document.getElementById('snp2').value=document.getElementById('snp2').value.toUpperCase(); 
 
 if(document.form1.snp2.value.length==4)
 {
  document.form1.snp3.focus();
 }
}
function snp3_onkeyup() 
{
 document.getElementById('snp3').value=document.getElementById('snp3').value.toUpperCase(); 
  
 if(document.form1.snp3.value.length==4)
 {
  document.form1.snp4.focus();
 }
}

function snp4_onkeyup() 
{
 document.getElementById('snp4').value=document.getElementById('snp4').value.toUpperCase();  
}

function snb1_onkeyup() 
{
 document.getElementById('snb1').value=document.getElementById('snb1').value.toUpperCase(); 
 
 if(document.form1.snb1.value.length==4)
 {
  document.form1.snb2.focus();
 }
} 
function snb2_onkeyup() 
{
 document.getElementById('snb2').value=document.getElementById('snb2').value.toUpperCase(); 
  
 if(document.form1.snb2.value.length==4)
 {
  document.form1.snb3.focus();
 }
}
function snb3_onkeyup() 
{
 document.getElementById('snb3').value=document.getElementById('snb3').value.toUpperCase(); 
  
 if(document.form1.snb3.value.length==4)
 {
  document.form1.snb4.focus();
 }
}
function snb4_onkeyup() 
{
 document.getElementById('snb4').value=document.getElementById('snb4').value.toUpperCase(); 
}

function snh1_onkeyup() 
{
 document.getElementById('snh1').value=document.getElementById('snh1').value.toUpperCase(); 
 
 if(document.form1.snh1.value.length==4)
 {
  document.form1.snh2.focus();
 }
} 
function snh2_onkeyup() 
{
 document.getElementById('snh2').value=document.getElementById('snh2').value.toUpperCase(); 
 
 if(document.form1.snh2.value.length==4)
 {
  document.form1.snh3.focus();
 }
}
function snh3_onkeyup() 
{
 document.getElementById('snh3').value=document.getElementById('snh3').value.toUpperCase(); 
 
 if(document.form1.snh3.value.length==4)
 {
  document.form1.snh4.focus();
 }
}
function snh4_onkeyup() 
{
 document.getElementById('snh4').value=document.getElementById('snh4').value.toUpperCase(); 
}



function return_to_signup()
{
	self.location.href = "membership.php";
}

/*for sn4 keyup*/
function sn41_onkeyup() 
{
 document.getElementById('sn41').value=document.getElementById('sn41').value.toUpperCase(); 
 
 if(document.form1.sn41.value.length==4)
 {
  document.form1.sn42.focus();
 }
} 
function sn42_onkeyup() 
{
 document.getElementById('sn42').value=document.getElementById('sn42').value.toUpperCase(); 
 
 if(document.form1.sn42.value.length==4)
 {
  document.form1.sn43.focus();
 }
}
function sn43_onkeyup() 
{
 document.getElementById('sn43').value=document.getElementById('sn43').value.toUpperCase(); 
 
 if(document.form1.sn43.value.length==4)
 {
  document.form1.sn44.focus();
 }
}
function sn44_onkeyup() 
{
 document.getElementById('sn44').value=document.getElementById('sn44').value.toUpperCase(); 
}
/*end*/
/*for sn5 keyup*/
function sn51_onkeyup() 
{
 document.getElementById('sn51').value=document.getElementById('sn51').value.toUpperCase(); 
 
 if(document.form1.sn51.value.length==4)
 {
  document.form1.sn52.focus();
 }
} 
function sn52_onkeyup() 
{
 document.getElementById('sn52').value=document.getElementById('sn52').value.toUpperCase(); 
 
 if(document.form1.sn52.value.length==4)
 {
  document.form1.sn53.focus();
 }
}
function sn53_onkeyup() 
{
 document.getElementById('sn53').value=document.getElementById('sn53').value.toUpperCase(); 
 
 if(document.form1.sn53.value.length==4)
 {
  document.form1.sn54.focus();
 }
}
function sn54_onkeyup() 
{
 document.getElementById('sn54').value=document.getElementById('sn54').value.toUpperCase(); 
}
/*end*/

/*request did page*/
function save_request_did()
{
	var full_name = document.getElementById("full_name");
	var email = document.getElementById("email");
	var usr_name = document.getElementById("usr_name");
	var country = document.getElementById("country");
	var snp1 = document.getElementById("snp1");
	var snp2 = document.getElementById("snp2");
	var snp3 = document.getElementById("snp3");
	var snp4 = document.getElementById("snp4");
	var model = document.getElementById("model");
	var city_did = document.getElementById("city-did");
	var country_did = document.getElementById("country-did"); 	
	var verify_code = document.getElementById("verify_code");
	var randval = document.getElementById("randval");
	
	var pattern_lsc = /^(\w{4})$/;
	var pattern_did = /^([0-9]|-)*$/;

	if(full_name.value=="")
	{
		alert("Please input your Full Name.");
		return false;
	}
	
	if(email.value=="")
	{
		alert("Please input Email Address.");
		return false;
	}
	else
	{
		if (isEmails(email.value) == false)
	  {
		alert("Email Address format error.");
		return false;
	  }
	}
	
	if(usr_name.value == "")
	{
		alert("Please input User Name.");
		return false;
	}
	
	if(country.value == "")
	{
		alert("Please input Your Country.");
		return false;
	}
	
	if(city_did.value=="")
	{
		alert("Please input City of DID Number.");
		return false;
	}
	else
	{
		if(!pattern_did.test(city_did.value))
		{
			alert("City of DID Number format error");
			return false;
		}
	}
	
	if(country_did.value=="")
	{
		alert("Please input Country of DID Number.");
		return false;
	}
	else
	{
		if(!pattern_did.test(country_did.value))
		{
			alert("Country of DID Number format error");
			return false;
		}
	}
	
	if(snp1.value=="" || snp2.value=="" || snp3.value=="" || snp4.value=="")
	{
		alert("Please input IPX Serial Number.");
		return false;
	}
	else
  {
    if(!check_sn(snp1.value,snp2.value,snp3.value,snp4.value))
    {
	    alert("IPX Serial Number format error,Serial Number can be found on the back of IPX product.");
	    return false;
    }
  }
  
  if(!check_sn_model(snp3.value,model.value))
  {
  	  alert("IPX Serial Number doesn't match with model");
		  return false;
  }
 
  if(verify_code.value == "")
  {
  	alert("Please input verification code.");
  	return false;
  }

  if(verify_code.value != randval.value)
	{
		alert("Verfication Code error.");
/*	var form_obj1 = document.getElementById("form1");
	  form_obj1.action = "request-did.php?cmd=unclear";
	  form_obj1.submit();*/
	  return false;
	}

	return true;
}

/*ticket page*/
function save_ticket()
{
	var full_name = document.getElementById("full_name");
	var email = document.getElementById("email");
	var snp1 = document.getElementById("snp1");
	var snp2 = document.getElementById("snp2");
	var snp3 = document.getElementById("snp3");
	var snp4 = document.getElementById("snp4");
	var model = document.getElementById("model");
	var subject = document.getElementById("subject");
	
	var pattern_lsc = /^(\w{4})$/;

	if(full_name.value=="")
	{
		alert("Please input your Full Name.");
		return false;
	}
	
	if(email.value=="")
	{
		alert("Please input Email Address.");
		return false;
	}
	else
	{
		if (isEmails(email.value) == false)
	  {
		alert("Email Address format error.");
		return false;
	  }
	}
	
	if(snp1.value=="" || snp2.value=="" || snp3.value=="" || snp4.value=="")
	{
		alert("Please input IPX Serial Number.");
		return false;
	}
	else
  {
    if(!check_sn(snp1.value,snp2.value,snp3.value,snp4.value))
    {
	    alert("IPX Serial Number format error,Serial Number can be found on the back of IPX product.");
	    return false;
    }
  }
  
  if(!check_sn_model(snp3.value,model.value))
  {
  	  alert("IPX Serial Number doesn't match with model");
		  return false;
  }

  if(subject.value == "")
  {
  	alert("Please input Subject");
  	return false;
  }
  
	return true;
}

/*lost_pwd page*/
function save_lost_pwd()
{
	var user_name = document.getElementById("usr_name");
	var email = document.getElementById("email");
	var snp1 = document.getElementById("snp1");
	var snp2 = document.getElementById("snp2");
	var snp3 = document.getElementById("snp3");
	var snp4 = document.getElementById("snp4");
	var model = document.getElementById("model");
	var info_type = document.getElementById("type");
	var pattern_lsc = /^(\w{4})$/;

	if(user_name.value == "" && email.value == "" && (snp1.value == "" && snp2.value == "" && snp3.value == "" && snp4.value == ""))
	{
		alert("Please input User Name or Email Address or IPX Serial Number.");
		return false;
	}
	
	if(user_name.value != "" && email.value == "" && 
	   (snp1.value == "" && snp2.value == "" && snp3.value == "" && snp4.value == ""))
	{
		info_type.value = 1;		 
	} 
	else if(user_name.value == "" && email.value != "" && 
	   (snp1.value == "" && snp2.value == "" && snp3.value == "" && snp4.value == ""))
	{
	  if (isEmails(email.value) == false)
	  {
		 alert("Email Address format error.");
		 return false;
	  }
	  else
	  {
	  	info_type.value = 2;	
	  }
	}
	else if(user_name.value == "" && email.value == "" && 
		(snp1.value !="" && snp2.value !="" && snp3.value !="" && snp4.value != ""))
	{
		/*if(!pattern_lsc.test(snp1.value) || !pattern_lsc.test(snp2.value)
       || !pattern_lsc.test(snp3.value) || !pattern_lsc.test(snp4.value)
      )*/
    if(!check_sn(snp1.value,snp2.value,snp3.value,snp4.value))
    {
	    alert("IPX Serial Number format error,Serial Number can be found on the back of IPX product.");
	    return false;
    }
    else if(!check_sn_model(snp3.value,model.value))
    {
    	alert("IPX Serial Number doesn't match with model");
    	return false;
    }
    else
    {
    	info_type.value = 3;	
    }
	}
	else
	{
		alert("You can select User Name or Email Address or IPX Serial Number,one of them!");
		return false;
	}
	
  var form_obj1 = document.getElementById("form1");
	form_obj1.action = "save_lost_pwd.php";
	form_obj1.submit();
	return true;
}

function return_to_lostpwd()
{
	self.location.href = "lost-pwd.php";
}

/*myaccount page*/
function save_myaccount()
{
	var usr_name = document.getElementById("usr_name");
	var usr_pwd = document.getElementById("pwd");
	var verify_code = document.getElementById("verify_code");
	var randval = document.getElementById("randval");
	
	if(usr_name.value=="")
	{
		alert("Please input your User Name.");
		return false;
	}
	
	if(usr_pwd.value=="")
	{
		alert("Please input Password.");
		return false;
	}
	  
  if(verify_code.value == "")
  {
  	alert("Please input verification code.");
  	return false;
  }
  
  if(verify_code.value != randval.value)
	{
		alert("Verfication Code error.");
		var form_obj1 = document.getElementById("form1");
	  form_obj1.action = "myaccount.php?cmd=unclear";
	  form_obj1.submit();
	  return false;
	}
	
	return true;
}
/*used in index page*/
function dyniframesize(iframename) {
  var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
  //extra height in px to add to iframe in FireFox 1.0+ browsers
  var FFextraHeight=getFFVersion>=0.1? 16 : 0 
  var pTar = null;
  if (document.getElementById){
    pTar = document.getElementById(iframename);
  }
  else{
    eval('pTar = ' + iframename + ';');
  }
  if (pTar && !window.opera){
    //begin resizing iframe
    pTar.style.display="block"
    
    if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight){
      //ns6 syntax
      pTar.height = pTar.contentDocument.body.offsetHeight+FFextraHeight; 
    }
    else if (pTar.Document && pTar.Document.body.scrollHeight){
      //ie5+ syntax
      pTar.height = pTar.Document.body.scrollHeight;
    }
  }
}

function link_to(url,target)
{
	eval(target+".location.href = \""+url+"\";");
}

function toupper(content)
{
	document.getElementById(content).value=document.getElementById(content).value.toUpperCase(); 
}

function goto_billing()
{
	window.open ('./login_check.php?from=myipx','loginwindow','height=800,width=1024,top=150,left=100,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');
}

function chat_to_email_1()
{
	var form_obj1 = document.getElementById("form1");
	form_obj1.action = "chat.php?support=1";
	form_obj1.submit();
	return true;
}

function chat_to_email_2()
{
	var form_obj1 = document.getElementById("form1");
	form_obj1.action = "chat.php?support=2";
	form_obj1.submit();
	return true;
}

function chat_to_email_3()
{
	var form_obj1 = document.getElementById("form1");
	form_obj1.action = "chat.php?support=3";
	form_obj1.submit();
	return true;
}

function chat_to_email_4()
{
	var form_obj1 = document.getElementById("form1");
	form_obj1.action = "chat.php?support=4";
	form_obj1.submit();
	return true;
}

function save_chat()
{
	var usr_name = document.getElementById("usr_name");
	var email = document.getElementById("email");
	var snp1 = document.getElementById("snp1");
	var snp2 = document.getElementById("snp2");
	var snp3 = document.getElementById("snp3");
	var snp4 = document.getElementById("snp4");
	var model = document.getElementById("model");
	
	if(usr_name.value=="")
	{
		alert("Please input your User Name.");
		return false;
	}
	
	if(email.value=="")
	{
		alert("Please input Email Address.");
		return false;
	}
	
	if(snp1.value!="" || snp2.value!="" || snp3.value!="" || snp4.value!="")
	{
		if(!check_sn(snp1.value,snp2.value,snp3.value,snp4.value))
    {
	    alert("IPX Serial Number format error,Serial Number can be found on the back of IPX product.");
	    return false;
    }
    
    if(!check_sn_model(snp3.value,model.value))
    {
  	  alert("IPX Serial Number doesn't match with model");
		  return false;
    }
	}
}
