﻿// JScript File
function replaceString(sExpression,sFind,sReplacement) {
// Replaces sFind with sReplacement in the string sExpression
   for (var i=0; i<sExpression.length; i++)  {
      if (sExpression.substring(i,i+sFind.length) == sFind) {
          sExpression = sExpression.substring(0,i)+sReplacement+sExpression.substring(i+sFind.length,sExpression.length)
          }   
      }   
      return sExpression
}

function SetCompany(CompID,CompName,CompAddr,CompCity,CompProv,CompPC) {
var objArray;
try
{
    window.opener.document.forms[0].ctl00$ContentsHere$txtCompanyID.value=CompID;
    window.opener.document.forms[0].ctl00$ContentsHere$txtCName.value=replaceString(CompName,"^","'");
    window.opener.document.forms[0].ctl00$ContentsHere$txtAddress.value=replaceString(CompAddr,"^","'");
    window.opener.document.forms[0].ctl00$ContentsHere$txtCity.value=replaceString(CompCity,"^","'");
    window.opener.document.forms[0].ctl00$ContentsHere$txtPostalCode.value=replaceString(CompPC,"^","'");
    objArray=window.opener.document.forms[0].ctl00$ContentsHere$lstProv;
}
catch(err){}
try
{
    window.opener.document.forms[0].txtCompanyID.value=CompID;
    window.opener.document.forms[0].txtCName.value=replaceString(CompName,"^","'");
    window.opener.document.forms[0].txtAddress.value=replaceString(CompAddr,"^","'");
    window.opener.document.forms[0].txtCity.value=replaceString(CompCity,"^","'");
    window.opener.document.forms[0].txtPostalCode.value=replaceString(CompPC,"^","'");
    objArray=window.opener.document.forms[0].lstProv;
}
catch(err){}


var i;
for(i=0;i<objArray.length;i++)
{
    if(objArray.options[i].value==CompProv)
        {
            objArray.options[i].selected=true;
        }
}
window.close();
}

function checkAll() {
for (var j = 1; j <= 14; j++) {
box = eval("document.checkboxform.C" + j); 
if (box.checked == false) box.checked = true;
   }
}

function uncheckAll() {
for (var j = 1; j <= 14; j++) {
box = eval("document.checkboxform.C" + j); 
if (box.checked == true) box.checked = false;
   }
}

function switchAll() {
for (var j = 1; j <= 14; j++) {
box = eval("document.checkboxform.C" + j); 
box.checked = !box.checked;
   }
}
function SelectAllCheckboxes(spanChk){

   // Added as ASPX uses SPAN for checkbox
   
   var oItem = spanChk.children;
   var theBox= (spanChk.type=="checkbox") ? 
        spanChk : spanChk.children.item[0];
   xState=theBox.checked;
   elm=theBox.form.elements;

   for(i=0;i<elm.length;i++)
     if(elm[i].type=="checkbox" && 
              elm[i].id!=theBox.id)
              
     //alert(theBox.id);
     
     {
       //elm[i].click();
       if(elm[i].checked!=xState)
         elm[i].click();
       //elm[i].checked=xState;
     }
 }
 
function prodwin(theURL, Name, arg) { 
var popW;
var popH;
if (arg=="Order")
    { popW = screen.width; popH = screen.height;}
else if (arg=="Email")
    { popW = 500; popH = 500;}
else
    { popW = 10; popH = 10;}
var winleft = (screen.width - popW) / 2;
var winUp = (screen.height - popH) / 2;
popH = popH - 130;
popW = popW - 10;
winProp = 'width='+popW+',height='+popH+',left='+winleft+',top='+winUp+',scrollbars=yes,resizable=yes,status=yes,toolbar=yes'
Win = window.open(theURL, Name, winProp)
if (parseInt(navigator.appVersion) >= 4) { Win.window.focus(); }
}

function clear_radio_buttons(obj) {
     for (var i = 0; i < obj.length; i++) {
          obj[i].checked = false;
     }
	 return false;
}


function doSubmit(btnName, Validate, Msg)
{	//this function loop through all "INPUT", "SELECT" and "TEXTAREA" element to validate
	// depending on value of "Formula".
	//"IsRequired" and "Rule" are required in HTML element.
	var FormOk=true;
	var objArray;
	var i, Rule, sBrace, Formula, SetFocus=null;
	var SubmitAction, Update_Warning_Msg;
	
	objArray=document.getElementsByName("SubmitAction");
	SubmitAction=objArray[0];
	if(SubmitAction) Update_Warning_Msg=SubmitAction.getAttribute("Msg");
	
	if(Update_Warning_Msg!=null && Update_Warning_Msg!='')
	{ if(!confirm(Update_Warning_Msg))return false;
	}

    if(Validate)
    {	objArray=document.getElementsByTagName("INPUT");
    	for(i=0;i<objArray.length;i++)
    	{	if(objArray[i].getAttribute("IsRequired")=="Yes" )
		    {	Rule=objArray[i].getAttribute("Rule");
    			if(Rule){
				    sBrace=Rule.indexOf("(");
				    if(sBrace>-1)
				    {	Formula=Rule.substring(0,sBrace);
    					Param=Rule.substring(sBrace+1,Rule.length-1);
					    switch(Formula)
					    {	case "IsEmail":
							    if(!IsValidEmail(objArray[i])) FormOk=false; break;
						    case "IsLength":
    							if(!IsLength(objArray[i],Param)) FormOk=false; break;
						    case "IsNumber":
    							if(!IsNumber(objArray[i],Param)) FormOk=false; break;
						    case "IsDate":
    							if(!IsDate(objArray[i])) FormOk=false; break;
						    case "IsChecked":
    							if(!IsChecked(objArray[i],Param)) FormOk=false; break;
					    }
					    if(SetFocus==null && !FormOk) SetFocus=objArray[i];
				    }
			    }
		    }
	    }
    
    	objArray=document.getElementsByTagName("SELECT");
    	for(i=0;i<objArray.length;i++)
    	{	if(objArray[i].getAttribute("IsRequired")=="Yes" )
		    {	Rule=objArray[i].getAttribute("Rule");
    			if(Rule){
				    sBrace=Rule.indexOf("(");
				    if(sBrace>-1)
				    {	Formula=Rule.substring(0,sBrace);
    					Param=Rule.substring(sBrace+1,Rule.length-1);
					    switch(Formula)
					    {	case "IsLength": //??
							    if(!IsLength(objArray[i],Param)) FormOk=false; break;
						    case "IsNumber": //??
    							if(!IsNumber(objArray[i],Param)) FormOk=false; break;
						    case "IsSelected":
    							if(!IsSelected(objArray[i])) FormOk=false; break;    							
						    case "IsDateDMY":
    							if(!IsDateDMY(objArray[i],Param)) FormOk=false; break;
    
					    }
					    if(SetFocus==null && !FormOk) SetFocus=objArray[i];
				    }
			    }
		    }
	    }
        
    
    	objArray=document.getElementsByTagName("TEXTAREA");
    	for(i=0;i<objArray.length;i++)
    	{	if(objArray[i].getAttribute("IsRequired")=="Yes" )
		    {	Rule=objArray[i].getAttribute("Rule");
    			if(Rule){
				    sBrace=Rule.indexOf("(");
				    if(sBrace>-1)
				    {	Formula=Rule.substring(0,sBrace);
    					Param=Rule.substring(sBrace+1,Rule.length-1);
					    switch(Formula)
					    {	case "IsLength":
							    if(!IsLength(objArray[i],Param)) FormOk=false; break;
					    }
					    if(SetFocus==null && !FormOk) SetFocus=objArray[i];
				    }
			    }
		    }
	    }
    }
    else
    {
    	FormOk=true;
    }

   	if(FormOk) 
   	{   var SubmitValue="Submit";
   	    var btn;
   	    objArray=document.getElementsByName(btnName);
	    btn=objArray[0];
   	    if(btn) SubmitValue=btn.getAttribute("Action");
        if(SubmitAction)SubmitAction.value=SubmitValue;	
	    document.aspnetForm.submit();
	}
	else 
	{   alert(Msg);
	    if(SetFocus!=null && SetFocus.disabled==false) SetFocus.focus();	
	}
	return FormOk;
}

function doCancel(btnName)
{
	var objArray,SubmitAction;
	var SubmitValue="Cancel";
   	var btn;
   	objArray=document.getElementsByName(btnName);
	btn=objArray[0];	
	if(btn) SubmitValue=btn.getAttribute("Action");
	objArray=document.getElementsByName("SubmitAction");
	SubmitAction=objArray[0];
    if(SubmitAction)SubmitAction.value=SubmitValue;
	document.aspnetForm.submit();
    return true;
}

function doDelete(btnName,Msg)
{
	var objArray,SubmitAction;
	var SubmitValue="Delete";
   	var btn;
   	objArray=document.getElementsByName(btnName);
	btn=objArray[0];	
	if(btn) SubmitValue=btn.getAttribute("Action");
	objArray=document.getElementsByName("SubmitAction");
	SubmitAction=objArray[0];
    if(SubmitAction)SubmitAction.value=SubmitValue;
    
    if(Msg=='[DEFAUL_DELETE_MSG]') Msg='Are you sure to delete?';
    
    if(Msg!='')
	{   if(confirm(Msg))
        {   document.aspnetForm.submit();
            return true;
        }
	  else  
	    return false;
	}
	else
    {   document.aspnetForm.submit();
        return true;
    }
}


function IsLength(Obj,Param)
{	//Obj holds textbox object, and Param holds length, Mininum is 1.

	var Length=parseInt(Param);
	var ObjValue=Obj.value.replace(' ','');
	if(ObjValue=='')
		return ErrDiv(Obj);
	else
		return ClearDiv(Obj);
}

function IsValidEmail(Obj)
{	//Obj holds textbox object

	var AtSymbol=Obj.value.indexOf('@');
	var Period=Obj.value.lastIndexOf('.');
	var Space=Obj.value.indexOf(' ');
	var Length=Obj.value.length - 1;

	if((AtSymbol<1) || (Period <= AtSymbol+1) || (Period==Length) || (Space!=-1))
		return ErrDiv(Obj);
	else
		return ClearDiv(Obj);
}

function IsNumber(Obj,Param)
{	//Obj holds textbox object, and Param holds Min,Max values

	var Comma=Param.lastIndexOf(',');
	var Min=parseInt(Param.substring(0,Comma));
	var Max=parseInt(Param.substring(Comma+1,Param.length));

	if(isNaN(Obj.value) || Obj.value=='')
	{
		return ErrDiv(Obj);
	}
	else
	{
		if(Min==0 && Max==0)
		{
			return ClearDiv(Obj);
		}
		else
		{
			var ObjValue=parseInt(Obj.value);
			if(Min<=ObjValue && Max>=ObjValue)
				return ClearDiv(Obj);
			else
				return ErrDiv(Obj);
		}
	}
}



function IsDateDMY(Obj, ObjDMY)
{	//Obj hold DAY element, and ObjDMY holds preffix of day/month/year combo ids.
    if(IsValidDateCombo(Obj, ObjDMY)) return ClearDiv(Obj); else return ErrDiv(Obj);
}

function IsValidDateCombo(Obj, ObjDMY)
{
	//Obj hold DAY element, and ObjDMY holds preffix of day/month/year combo ids.
	ObjDMY = ObjDMY.replace('\'','');
	ObjDMY = ObjDMY.replace('\'','');
	ObjDay = document.getElementById(ObjDMY + "Day");
	ObjMonth = document.getElementById(ObjDMY + "Month");
	ObjYear = document.getElementById(ObjDMY + "Year");

	if(ObjDay && ObjMonth && ObjYear)
	{
		day=ObjDay.value;
		month=ObjMonth.value;
		year=ObjYear.value;
		if(day>0 && month>0 && year>0)
		{
			var dtDate;
			//javascript months start at 0 (0-11 instead of 1-12)
			month=month-1;
			dtDate=new Date(year, month, day);

			/*Javascript Dates are a little too forgiving and will change the date to a reasonable
			guess if it's invalid. We'll use this to our advantage by creating the date object and
			then comparing it to the details we put it. If the Date object is different, then
			it must have been an invalid date to start with.*/
			IsValidDate=false;
			IsValidDate=((day==dtDate.getDate()) && (month==dtDate.getMonth()) && (year==dtDate.getFullYear()));

			return IsValidDate;
		}
		else
			return false;
	} else return false;
}


function IsChecked(Obj,Param)
{	//Obj holds textbox object, and Param holds length, Mininum is 1.

	var objArray;
	var i,cntr=0;
	var GroupName;

	if(Obj.type=='radio')
	{	objArray=document.getElementsByName(Obj.name);
    	for(i=0;i<objArray.length;i++)
    		if(objArray[i].checked)	cntr++;
	}

	if(Obj.type=='checkbox')
	{	GroupName=Obj.getAttribute("GroupName");
	    objArray=document.getElementsByTagName("INPUT");
    	for(i=0;i<objArray.length;i++)
	       if(GroupName==objArray[i].getAttribute("GroupName"))
		        if(objArray[i].checked)	cntr++;
	}

	if(cntr>=parseInt(Param))
		return ClearDiv(Obj);
	else
		return ErrDiv(Obj);
}

function IsSelected(Obj)
{	//Obj holds combobox object

    if(Obj.value=='0')
		return ErrDiv(Obj);	
	else
		return ClearDiv(Obj);
}

function IsCanadianPC(Obj,CountryName)
{	//Obj holds textbox object, and CountryName should be "Canada".

	var objRegExp  = /^\D{1}\d{1}\D{1}\d{1}\D{1}\d{1}/;
	if(CountryName=='Canada')
	{   if(objRegExp.test(Obj.value) && Obj.value.length==6)
	        return ClearDiv(Obj);
	    else
	        return ErrDiv(Obj);
	}
	else
	{
	    return ClearDiv(Obj);
	}
}


function ErrDiv(Obj)
{	//Obj holds textbox object, as id is required for show/hide related DIV.

	divObj = document.getElementById("divErr" + Obj.id);
	if(divObj && Obj) divObj.innerHTML = "&nbsp;" + Obj.getAttribute("Msg");
	return false;
}

function ClearDiv(Obj)
{	//Obj holds textbox object, as id is required for CLEAR related DIV.
	divObj = document.getElementById("divErr" + Obj.id);
	if(divObj) divObj.innerHTML = '';
	return true;
}

function ErrDivId(name)
{	//id is required for show/hide related DIV.
    Obj = document.getElementsByName(name);
	divObj = document.getElementById("divErr" + name);
	if(divObj && Obj[0]) divObj.innerHTML = "&nbsp;" + Obj[0].getAttribute("Msg");
	return false;
}

function GoToNext(now,next,maxLength)
{
    var letters = now.value.length+1;
    now.value = now.value.toUpperCase()
    if (letters <= maxLength)
        now.focus();
    else
        next.focus();
}

function ToUpper(Obj)
{
    Obj.value = Obj.value.toUpperCase();
}

function ValidatePostalCode(Obj)
{
    var res = "";
    var code = Obj.value;

    ClearDiv(Obj);

    // only letters and numbers are allowed
    for(var i = 0; i < code.length; i++)
    {
        if (code.charAt(i) >= 'A' && code.charAt(i) <= 'Z' || code.charAt(i) >= '0' && code.charAt(i) <= '9')
        {
             res += code.charAt(i);
        }
    }
    
    // if max length is reached then validate postal code
    if(res.length == 6)
    {
	    var objRegExp  = /^\D{1}\d{1}\D{1}\d{1}\D{1}\d{1}/;
	    if (!objRegExp.test(Obj.value))
            ErrDiv(Obj);
	}
    
    Obj.value = res;
}

function RemoveRestrictedCharacters(Obj)
{
    var res = "";
    var code = Obj.value;

    // only letters and numbers are allowed
    for(var i = 0; i < code.length; i++)
    {
        if (code.charAt(i) >= 'A' && code.charAt(i) <= 'Z' || code.charAt(i) >= '0' && code.charAt(i) <= '9')
        {
             res += code.charAt(i);
        }
    }
    
    Obj.value = res;
}
