﻿// JScript File
// Templete for Selected elements in a Selection Grid
function selectedElement(name, value)
{
  this.name = name
  this.value = value
}

// Update form widgets based on objects in arrSelection.
// objects in arrSelection is expected to have properties 'name' and 'value'
        function updateWidgets(arrSelection, cSrcCtrlID)
        {
            var cstr;
            var item;
            var ctrl
            cstr = '';
            for (item in arrSelection)
            {
                //cstr += arrSelection[item].name+' '+arrSelection[item].value + ' '
                ctrl = document.getElementById(arrSelection[item].name)
                if (ctrl != null )
                {
                   //alert(ctrl.type)
                   switch (ctrl.type)
                    {
                    case 'text':
                        //alert(ctrl.name)
                        ctrl.value = arrSelection[item].value
                        break
                    break
                    default:
                         ctrl.value = arrSelection[item].value
                    }
                }
            }
            return cstr 
         }
         

function trim(inputString) {
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; 
};

function PopUpCalendar( cFormName, cWinName )
{
		top_pos = window.event.screenY;
 	    left_pos = window.event.screenX;
 	    cal_width = 205;
 	    cal_height = 225;
 	    cfrmName = 'CalendarLookup.aspx?formname='+ cFormName
 	     	    
 	    if ( (top_pos + cal_height) > screen.height)
 	    {
 			top_pos = screen.height - cal_height - 30;
 	    }
 	    if ((left_pos + cal_width) > screen.width )
 	    {
 			left_pos = screen.width - cal_width - 30;
 	    }
 	    cOpenProperties = 'width='+cal_width+ ',height='+ cal_height +',top='+top_pos+',left='+left_pos
		cw=window.open( cfrmName, cWinName, cOpenProperties );
        return 0;
};


function PopUpHelp( cHelpID )
{
		top_pos = window.event.screenY;
 	    left_pos = window.event.screenX;
 	    help_width = 770;
 	    help_height = 500;
	    cfrmName = 'Help.aspx?helpId='+ cHelpID
	     	     	    
 	    if ( (top_pos + help_height) > screen.height)
 	    { 
 			top_pos = screen.height - help_height - 30;
 	    }
 	    if ((left_pos + help_width) > screen.width )
 	    {
 			left_pos = screen.width - help_width - 30;
 	    }
		cOpenProperties = 'width='+help_width+ ',height='+ help_height +',top='+top_pos+',left='+left_pos + ', resizable=yes, scrollbars=yes';
		cw=window.open( cfrmName, 'OnlineHelp', cOpenProperties );				
        return false; 
};


function CloseCurrWindow()
{
	//window.opener = window;
	//window.opener.close();
	window.close(); //working version
};


function CloseCurrWindow2() // This is only a test. It is not used anywhere.
{
	//var win = window.opener;
	//win.close;
	alert(window.opener)
	window.opener.close('RemindPassword.aspx');
};


// ******************************************************************
// This function accepts a string variable and verifies if it is a
// proper date or not. It validates format matching either
// dd-mm-yyyy or dd/mm/yyyy. Then it checks to make sure the month
// has the proper number of days, based on which month it is.

// The function returns true if a valid date, false if not.
// KXA 11 Sep 2003 : Copied from the net.
// ******************************************************************

function isDate(dateStr) {

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {
alert("Please enter date as either dd/mm/yyyy or dd-mm-yyyy.");
return false;
}

//month = matchArray[1]; // p@rse date into variables
//day = matchArray[3];
//year = matchArray[5];

month = matchArray[3]; // p@rse date into variables
day = matchArray[1];
year = matchArray[5];

if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}

if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn`t have 31 days!")
return false;
}

if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
alert("February " + year + " doesn`t have " + day + " days!");
return false;
}
}
return true; // date is valid
}

//************************************************************************
//KXA:11 Sep 2003 : Created Function 
//If the date passed in string parameter dateStr, ends with 2 digit year this function will convert it to 4 digit year
//Make sure the date is a valid date in the format dd/mm/yy or dd/mm/yyyy 
//before calling this function.

function make4DigitYear(dateStr) {
   var myStr = dateStr;
   var idx
   var idx2
   
   idx = myStr.indexOf("/")
   if (idx == -1)
   {
    idx = myStr.indexOf("-")
    if (idx != -1 )
    {
      myStr = myStr.substr( idx+1, 15)
      idx2 = myStr.indexOf("-")
    }
   }
   else
   {
      myStr = myStr.substr( idx+1, 15)
	  idx2 = myStr.indexOf("/")   
   }

   if (idx != -1 && idx2 != -1 )
   {
       myStr = myStr.substr( idx2+1, 15)
       if (myStr.length == 2)
       {
		  return dateStr.substr(0,idx) + "/" + dateStr.substr(idx+1, idx+idx2-2 )+"/"+"20"+ dateStr.substr(idx+idx2+2, 15 )	   		  
	   }
	   else
	   {
			return dateStr
	   }   
   }  
   else
   {
      return dateStr
   }
}

function createDate(dateStr)
{
   var myDate  = new Date()
   var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
   var matchArray = dateStr.match(datePat); // is the format ok?
   var myDate 
   var month = 0
   var date = 0
   var year = 0
      
   if (matchArray == null) {
       alert("Please enter date as either dd/mm/yyyy or dd-mm-yyyy.");
       return myDate;  
   }
   date = matchArray[1];
   month = matchArray[3]; // p@rse date into variables
   year = matchArray[5];
   myDate = new Date(year, month - 1, date )	
   return myDate;      
}

function dateCompare(date1, date2)
{
   var year1 = 0
   var year2 = 0
   var mon1  = 0
   var mon2	 = 0
   var day1  = 0
   var day2  = 0
       
   year1 = date1.getFullYear()
   year2 = date2.getFullYear()  
   mon1  = date1.getMonth()
   mon2	 = date2.getMonth()     
   day1  = date1.getDate()
   day2  = date2.getDate()
      
   if ( year1 > year2) 
   {
        return 1 //Date1 is greater        
   }
   else
   {
      if ( ( mon1 > mon2) && ( year2 == year1 ) )  
      {
        return 1 //Date1 is greater        
      }
      else
      {
         if ( day1 > day2 && (year2 == year1 ) && ( mon1 = mon2) )
         {
            return 1 //Date1 is greater            
         }
         else 
         {
            if ( year1 == year2 && mon1 == mon2 && day1 ==  day2 )
            { 
                return 0  //Both dates are equal
             }   
            else 
            {
			   return -1 //Date1 is smaller then date2	
            }
         }
      }
   }
}

function validNameStr( cName )
{
	var valid = true;
	var ctmpName = trim(cName);
	var nlen= ctmpName.length;
	var ch;
	
    if (typeof cName != "string") { return false; }	
    if (nLen = 0 ) { return false ;}
	
	ctmpName = ctmpName.toUpperCase();
    while ( (nlen > 0) && valid ) 
    { // Check for spaces at the beginning of the string
        ch = ctmpName.substr(nlen-1, 1);
        if ( (ch < "A" ) || (ch> "Z") )
        {
			if ( (ch != "'") && (ch != "-") && (ch != " ") )
			{
        		valid = false;	     	
	        }
        }       
        nlen -= 1;
    }
	return valid
}		

function capitalise1StChar(cStr)
{
	var prevChar = "";
	var ctmpStr = "";
	var nlen= cStr.length;
	var ch = "";
	var nI = 0;
	
    if (typeof cStr != "string") { return cStr; }	
    if (nLen = 0 ) { return cStr ;}
    
    while ( (nI < nlen ) )     
	{
		ch =  cStr.substr(nI, 1);
		
		if ( ( prevChar == "" ) || ( prevChar == "-") || (prevChar == " ") || (prevChar == "'") )
		{
            ctmpStr += ch.toUpperCase();		         
		}				
		else
		{
		    ctmpStr += ch;
		}
        prevChar = ch;				        	
        nI += 1;
	}         
	return ctmpStr;
}

function queryString(ji, _default) 
{
   hu = window.location.search.substring(1);
   gy = hu.split("&");
   res = 0;
   
   
   for (i=0;i<gy.length;i++)
   {
      ft = gy[i].split("=");
      if (ft[0] == ji) 
      {
         res  = ft[1];
      }
   }
   return res;
}

//kxa 01 May 2008 : Created function
function replaceParams(urlString)
{
    // replaces parameters in a URL with control values of controls with the same name
//     var strin = 'GenericForm.aspx?form=22239066&RULE_ID=@RULE_ID&test=sevem'
       var strin;
   // var strin = 'GenericForm.aspx'
     var qryString = urlString.split("?");
     var p = qryString.length;
     var qryParams;
     var arrParams;
     var arrParam;
     var value;
     var ctrl;

     if (p > 1)
     {   
            qryParams=qryString[1];
            //alert(qryParams);
            arrParams = qryParams.split("&");
            //alert(arrParams.length); 
            for (i=0; i<arrParams.length; i++)
            {
                   arrParam=arrParams[i].split("=");
                   value = arrParam[1]
                   if (value.indexOf("@") != -1 )
                   {
                       //alert("found one");
                       //alert(document.getElementById("RULE_ID"). value);
                       ctrl = document.getElementById(value.substring(1));
                       strin=urlString.replace(value,ctrl.value); 
                   }
             }  
     } else
     {
       strin=urlString;
     }
     return strin;
}




//kxa 01 May 2008 : Created function
function replaceParams(urlString)
{
    // replaces parameters in a URL with control values of controls with the same name
     var strin;
     var strin; 
     var qryString = urlString.split("?");
     var p = qryString.length;
     var qryParams;
     var arrParams;

     var arrParam;
     var value;
     var ctrl;

     strin=urlString;
     if (p > 1)
     {   
            qryParams=qryString[1];
            arrParams = qryParams.split("&");
            for (i=0; i<arrParams.length; i++)
            {
                   arrParam=arrParams[i].split("=");
                   value = arrParam[1];
                   if (value.indexOf("@") != -1 )
                   {
                       ctrl = document.getElementById(value.substring(1));
                       strin=strin.replace(value,ctrl.value); 
                   }
             }  
     } 
     return strin;
}




// Pop-up the Pro window for rapid address searching
function popupQASPro(QASServer, cCallBack )
{
    // var QASServer = "http://sii892/prowebVB/";
	// DEBUG: specify the initial country in DataId & function to call in Callback
	// alert(QASServer)
    var ProPopup = window.open(QASServer + "RapidSearch.aspx?DataID=&Callback="+cCallBack,
		"ProPopup",
		"scrollbars=no,resizable=yes,width=540,height=560");
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }


