/*
 To prepare a UAT version, create a variable 'rm_folder' = policy_uat that used  at function start()
 
*/
var gs_rm_folder = 'web_policy'
var gs_host = '218.103.17.141'
var d=new Date();
var thisYear=d.getFullYear();
//var path="http://localhost/concord";
//var path="http://www.widerworld.com.hk/concord/";
var path="http://www.concordinsurance.com.hk";
/*            A                 */

function alertCover()
{
	alert('if you want to fill in COVER NOTE NO, You must fill in the COVER NOTE NO before entering other data');
}

function autoTab(name,size,to)
{
	form=document.form1;
	//alert(eval("form."+name+".value").length);
	if (eval("form."+name+".value").length==size)
		eval("form."+to).focus();
}

/*            C                 */

function calDate(date,dif,para)
{
	theDate=new Date();
	theDate.setDate(dif);
	return (Math.floor(date.getTime()-theDate.getTime())/(1000*60*60*24));
}

function cancel()
{
	if (confirm('Are you sure to cancel ?'))
		window.close();
}

function checkClient(here)
{
	form=document.form1;
	if (here.name=="client_id")
		form.client_name.selectedIndex=form.client_id.selectedIndex;
	else if (here.name=="client_name")
		form.client_id.selectedIndex=form.client_name.selectedIndex;
	form.add_info.value=form.client_name.value;
}

function checkOnlyNumAndWord(formStr,value)
{
	var illegalChars = /\W/;
  // allow only letters, numbers, and underscores
 //alert(formStr.value);
 	//alert(formStr.value);
   if (illegalChars.test(formStr.value) & formStr.value.indexOf(" ")<0) {
       alert(value+" can input numbers and letters only");
	   select(formStr);
	   return true;
    }
	else
	return false;
}
function checkLegalChar(formStr,value)
{
   var illegalChars = /[^a-z0-9-_]/i;
   if (illegalChars.test(formStr.value)) {
       alert(value+" can input numbers, letters , hyphen and underscore only");
	   select(formStr);
	   return false;
    }
	else
	return true;
}

function checkPath()
{
		if (document.referrer.indexOf(path)==0)
		{
			return true;
		}
		if (self.opener)
		{
			if ((self.opener.location.toString()).indexOf(path)==0)
			{
				return true;
			}
		}
		return false;
}

function checkrefer()
{
	//alert(typeof(parent.location.toString()));
	/*test=parent.location.toString();
	if (!(document.referrer.indexOf(path)==0))
	{
		if (!(test.indexOf(path)==0))
			document.location=path;
			
	}*/
}

function confirmDelete()
{
	return confirm('Are you sure to delete ?');
}
function confirmVoid()
{
	return confirm('Are you sure to void this policy ?');
}
function confirmPopUp(url)
{
	if (url.indexOf("toDatabase")!=-1)
	{
		if (confirm("Are you sure to submit"))
			newPopUp(url);
	}else
		newPopUp(url);
	
}

function confirmPopUp2(url)
{
		if (confirm("Are you sure to submit"))
			goto(url);
	
}


/*            D                 */

function dateRange(date1,date2,value)
{
	
	var dateFrom=new Date(date1[2],(date1[1]-1),date1[0]);
	var dateTo=new Date(date2[2],(date2[1]-1),date2[0]);	
	//alert(new Date(2005,2,4));
	//alert(dateFrom+"    "+dateTo);
	if (dateFrom>dateTo)
	{
		alert(value);
		return true;
	}
	return false;
}

function dateRange2(date1,date2,value)
{
	//alert(date1[2]+'/'+date1[1]+'/'+date1[0]);
	var dateFrom=new Date(date1[2],(date1[1]-1),date1[0]);
	var dateTo=new Date(date2[2],(date2[1]-1),date2[0]);	
	//alert(new Date(2005,2,4));
	//alert(dateFrom+"    "+dateTo);
	if (dateFrom>=dateTo)
	{
		alert(value);
		return true;
	}
	return false;
}
function inDateRange(lowerdate,upperdate,ourdate,msg)
{
	var lowerdate=new Date(lowerdate[2],(lowerdate[1]-1),lowerdate[0]);
	var upperdate=new Date(upperdate[2]+1,(upperdate[1]-1),upperdate[0]);	
	var	ourdate  =new Date(ourdate[2],(ourdate[1]-1),ourdate[0]);
	var loweryear=lowerdate.getYear()+1900;
	var upperyear=upperdate.getYear()+1900;
	if (ourdate<upperdate && ourdate>=lowerdate)
	{
		//alert(msg);
		return true;
	}
	alert(msg);

	return false;
}
function disagree()
{
	if (confirm("Are you sure to disagree ?"))
		window.close();
}

function doCheckAll()
{
  with (document.formPop) {
	  if (select_all.value=="Select All")
	  {
	  		check=true;
			select_all.value="Deselect All";
	  }else if (select_all.value=="Deselect All")
	  {
	  		check=false;
			select_all.value="Select All";
	  }
    for (var i=0; i < elements.length; i++) {
        if (elements[i].type == 'checkbox')
		{
           elements[i].checked = check;
		}
    }
  }
}

function doSaveAs(para){
	/*form=document.form1;
	var len = form.elements.length
	for (var i = 0; i<len; i++) {
		var thisform = form.elements[i]
		if (thisform.type=="button")
		{
			thisform.style.visibility='hidden';
		}
	}*/
	//doit(para);
	//document.write(document.location);
	if (document.execCommand){
	if (isReady){document.execCommand("SaveAs",null,fileName+".html");}
	}else{
	alert('Feature available only in Internet Exlorer 4.0 and later.');
	}
	//location.href = location.href;
}



//	alert("hello");
	/*var head=document.getElementById(header).style;
	
	if (head.display=="none")
		head.display="";
	else
		head.display="none";*/

/*               F                  */

function fmtHTMLChar(form) {
	var len = form.elements.length
	for (var i = 0; i<len; i++) {
		var thisform = form.elements[i]
		if (thisform.type!="submit" && thisform.type!="button")
		while (thisform.value.indexOf("'") != -1 ||
				thisform.value.indexOf("<") != -1 ||
				thisform.value.indexOf(">") != -1 ||
				thisform.value.indexOf("%") != -1 ||
				thisform.value.indexOf("\"") != -1 ||
				thisform.value.indexOf("\\") != -1) {
		//alert (thisform.value);
			thisform.value = thisform.value.replace("'", "&#39;").replace("%", "&#37;").replace("<", "&lt;").replace(">", "&gt;").replace("\"", "&#34;").replace("\\", "&#92;")
		//alert (thisform.value);
		}
	}
}

/*               G                  */

function goto(link1)
{
	//alert(link1);
	document.location=link1;
	//self.opener.location=self.opener.location;
}

/*               I                  */

function inRange(inputStr, lo, hi) {
	var num = parseInt(inputStr, 10)
	if (num < lo || num > hi) {
		return false
	}
	return true
}

function in_array(theValue,theArray)
{
	//alert(theArray.length);
	for (j=0;j<theArray.length;j++)
	{
		//alert(theArray[j]);
		if (theArray[j]==theValue)
			return true;
	}
	return false;
}

function isEmail(email)
{
	var emailPattern = /^\w+[\w\.-]+@[\w\.-]+\.\w+$/g
	if (emailPattern.test(email.value)) 
	return true;	
	alert("Please enter valid email ");
	select(email)
	return false;
}

function isEmpty(inputStr) {
	if (isNaN(inputStr) || inputStr=="") {
		return true
	}
	return false
}

function isEmptyInForm(formStr,value) {
	//alert(formStr.name);
	if ((formStr.type=="text") || (formStr.type=="password") || (formStr.type=="select-one") || (formStr.type=="textarea") )
	{
	if (formStr.value == "" || formStr.value == null) {
	alert("Please enter "+value);
	select(formStr);
		return true
		}
	}
	else if (formStr[0].type=="radio")
	{
	//alert("here");
	Notok=true;
		for (var i=0; i < formStr.length; i++)
  	 	{
   		if (formStr[i].checked)
      		{
     		 Notok=false;
     		 }
		}
		if (Notok)
		{
			alert("Please choose "+value);
			//select(formStr);
		}
		return Notok
	}
	//return false
}

function isEmptyInFormTel(formStr,value) {
	Notok=true;
	for (i=0;i<formStr.length;i++)
	{
		//alert(formStr[i].type);
		if (formStr[i].type=="text")
			Notok=Notok&(formStr[i].value=="");
		else if (formStr[i].type=="checkbox" || formStr[i].type=="radio")
			Notok=Notok&(!formStr[i].checked);
	}
	if (Notok)
	{
	alert ("Please enter "+value);
	return true;
	}
	return false;
	//return false
}

function isMoney(money,theValue)
{
	//alert(name.value);
	testing=money;
	illegalChars=/[^0-9$,. ]/;
	if ((testing.value.match(illegalChars) && money.value.indexOf("-")==-1) || trim(money.value)=="" )
	{
		alert (theValue+" can enter numbers \"-\",\"$\" and \",\" only");
		select(testing);
		return false;
	}
	else
		return true;
}

function isName(name,theValue)
{
	//alert(name.value);
	illegalChars=/[^a-zA-Z ,.'"]/;
	if (name.value.match(illegalChars))
	{
		alert (theValue+" can enter letters only");
		select(name);
		return false;
	}
	else
		return true;
}

function isNum(num,theValue)
{
	//alert(name.value);
	//if (num.value.match(illegalChars) || trim(num.value)=="")
	//alert(isNaN(num.value)+"  "+parseFloat(num.value));
	//alert(parseFloat(num.value));
	if (parseFloat(num.value)==0 || isNaN(num.value))
	{
		if (num.value!=0)
		{
			alert (theValue+" can enter numbers only");
			select(num);
			return false;
		}else
			return true;
	}
	else
		return true;
}

function isPhone(phone,theValue)
{
	illegalChars=/[^0-9 -]/;
	if (phone.value=="")
	{
		return true;
	}
	if (phone.value.match(illegalChars))
	{
		alert (theValue+" can enter numbers,space and  \"-\"  only");
		select(phone);
		return false;
	}
	if (phone.value.length<8)
	{
		alert ("Please enter a valid "+theValue);
		select(phone);
		return false;
	}
	return true;
}


function isPrint()
{
	if (window.print)
    window.print();
}

/*               M                  */

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
 	   eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	  if (restore) selObj.selectedIndex=0;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

/*               N                  */

function newPopUp(url)
{
	window.open(url,'copy','width=1000, height=650, resizable=yes, scrollbars=yes,left = 0,top = 0');
}

function newPopUp2(url)
{
	window.open(url,'copy2','width=1000, height=650, resizable=yes, scrollbars=yes,left = 0,top = 0');
}

function notInputTogether(form,data1,data2)
{
	notTogether=false;
	Empty=true;
	for (i=0;i<data1.length;i++)
	{
		first=data2[0]+data1[i];
		second=data2[1]+data1[i];
		if (eval("form."+first+".value")!="" || eval("form."+second+".value")!="")
			{
				if (!(eval("form."+first+".value")!="" && eval("form."+second+".value")!=""))
				{
					notTogether=true;
				}
			}
		for (j=0;j<data2.length;j++)
		{
			third=data2[j]+data1[i];
			if (eval("form."+third+".value")!="")
				Empty=false;
			//alert(eval("form."+third+".value"));
			//alert(Empty);
		}
	}
	return new Array(notTogether,Empty)
}

/*               P                  */

function passportNo(formStr,value)
{
	var illegalChars = /\W/;
  // allow only letters, numbers, and underscores
 //alert(formStr.value);
 	//alert(formStr.value);
   if (illegalChars.test(formStr.value) ) {
       alert(value+" can input numbers and letters only");
	   select(formStr);
	   return true;
    }
	return false;
}

function printAlert()
{
	alert("Please press print in the window toolbar");
}


function printPopup(url)  
{
	window.open(url, 'residentialForm', 'width=650, height=650, resizable=no, scrollbars=yes,toolbar=yes');
}

//alert('here');
function printOncePopUp(policy,version,para,isDraft)  // the window will pop up when the policy can print once only and the policy number will record down
{
	window.open('printOnce.php?policy='+policy+'&version='+version+'&isDraft='+isDraft+'&ppa='+para,'printPopUp','width=250, height=100, resizable=yes, scrollbars=no,toolbar=no, top=0, left=0');
}

/*               R                  */

function reloading()
{
	document.location=document.location;
}

function roundNumber(value,dec) {
	var rlength =dec ; // The number of decimal places to round to
	var newnumber = Math.round(value*Math.pow(10,rlength))/Math.pow(10,rlength);
	return newnumber;
}

/*               S                  */

function sameAdd(var1,var2)
{
	form=document.form1;
	if (form.checkbox.checked)
	{
		//alert(eval("form."+var2+".value"));
		eval("form."+var1).value=eval("form."+var2).value;
		eval("form."+var1+"2").value=eval("form."+var2+"2").value;
		eval("form."+var1+"3").value=eval("form."+var2+"3").value;
	}
}


function select(field) {
	if (field != null) {
		field.focus()
		//field.select()
	}
}
function show()
{
	
}

function showButton()
{
	document.getElementById('SaveAs').style.visibility='visible';
}


function showThing(thing,para)
{
	if (self.opener)
	{
		alert(path+"/all#"+"   "+self.opener.location.href);
		if (self.opener.location.href!=path+"/all.php#")
			document.getElementById(thing).style.display=para;
	}
}


function start(id,version,type) {
	//alert(type.toString()=="endorse");
	id=trim(id);
  if (type=="policy")
  {
	 type="mc_policy";	  
  }else if (type=="cover")
  {
  	type="mc_coverNote";
  }else if (trim(type)=="endorse")
  {
	//  alert("hello");
  	type="mc_endorsement";
  }else if (trim(type)=="endorseC")
  {
	//  alert("hello");
  	 type="mc_cn_endorsement";
  }else if (trim(type)=="DHpolicy") // replace by 'dh_getprintlink' ,will not call anymore
  {
	//  alert("hello");
  	 type="dh_policy";
  }else if (trim(type)=="DHendorse") //replace by 'dh_getprintlink' ,will not call anymore
  {
	//  alert("hello");
  	  type="dh_endorsement";
  }
 	//return 'http://localhost/web_policy_dev/toPDF.asp?policy='+id+'&version='+version+'&type='+type;
	//return 'http://218.103.17.140/'+gs_rm_folder+'/toPDF.asp?policy='+id+'&version='+version+'&type='+type;


return 'http://'+gs_host+'/'+gs_rm_folder+'/toPDF.asp?policy='+id+'&version='+version+'&type='+type;
//window.open('http://localhost/web_policy_dev/toPDF.asp?policy='+id+'&version='+version+'&type='+type,'PDF','width=1000, height=650, resizable=yes, scrollbars=yes,left = 0,top = 0');
 	//window.open('http://218.103.17.140/web_policy_dev/toPDF.asp?policy='+id+'&version='+version+'&type='+type,'PDF','width=1000, height=650, resizable=yes, scrollbars=yes,left = 0,top = 0');
}

/*               T                  */

function trim(s) 
{
  // Remove leading spaces and carriage returns
 // alert(s);
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
  {
    s = s.substring(1,s.length);
  }

  // Remove trailing spaces and carriage returns

  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}


function textarea_valid(text,len,theValue)
{
	if (text.value.length>len || trim(text.value)=="")
	{
		alert (theValue+" cannot exceed "+len+" words");
		select(text);
		return false;
	}
		return true;
}

/*               U                  */

function unCheck(form)
{
	var len = form.elements.length
	for (var i = 0; i<len; i++) {
		var thisform = form.elements[i]
		if (thisform.type=="radio")
			thisform.checked=false;
	}
}


function unfmtHTMLChar(form) {
	var len = form.elements.length
	for (var i = 0; i<len; i++) {
		var thisform = form.elements[i]
		if (thisform.type!="submit" && thisform.type!="button" && thisform.type!="hidden")
		while (thisform.value.indexOf("&#39;") != -1 ||
				thisform.value.indexOf("&lt;") != -1 ||
				thisform.value.indexOf("&gt;") != -1 ||
				thisform.value.indexOf("&#37;") != -1 ||
				thisform.value.indexOf("&#34;") != -1 ||
				thisform.value.indexOf("&#92;") != -1) {
		//alert (thisform.value);
			thisform.value = thisform.value.replace("&#39;", "'").replace("&#37;", "%").replace("&lt;", "<").replace("&gt;", ">").replace("&#34;", "\"").replace("&#92;", "\\")
		//alert (thisform.value);
		}
	}
}

function unHTMLChar(value) {
	return value.replace("&#39;", "'").replace("&#37;", "%").replace("&lt;", "<").replace("&gt;", ">").replace("&#34;", "\"").replace("&#92;", "\\")
}

/*               V                  */


function validateDate(dfield, mfield, yfield) {
	//alert(dfield.value);
	if (isEmpty(dfield.value)) {
		alert("Please enter the date ");
		select(dfield)
		return false
	} 
		if (isEmpty(mfield.value)) {
			alert("Please enter the date ");
			select(mfield)
			return false
		} else {
			var monthVal = parseInt(mfield.value, 10)
			var yearVal = parseInt(yfield.value, 10)
			var monthMax = new Array(31,31,28,31,30,31,30,31,31,30,31,30,31)
			if ((yearVal-1900)%4 == 0) {				
					monthMax[2] = 29;				
			}
			var input = parseInt(dfield.value, 10)
			var top = monthMax[monthVal]
			if (!inRange(input,1,top)) {
			//alert(input);
				alert("Please enter between 1 and " + top + ".")
				select(dfield)
				//dfield.focus()
				return false
			}
		}
		 if (isEmpty(yfield.value)) {
			alert("Please enter the date ");
			select(yfield)
			return false
			}
			else
		if (mfield.value<1 || mfield.value>12)
		{
		alert("Please enter the correct date ");
		select(mfield)
		return false
		}
	return true
}

var head="display:''"
function $k(header){
	var head=header.style
	if (head.display=="none")
	head.display=""
	else
	head.display="none"
}
var isReady = true;
