﻿// JScript File
// Common javascript functions used
// ---------------------------------
var IE4 = (document.all);
var NS4 = (document.layers);
// ---------------------------------
var submitcount=0;
function mos(txt) { self.status = txt; document.body.style.cursor='pointer';}
function statClr() { self.status = ''; document.body.style.cursor='auto';}
function showhide(id,flg)
{
    if (document.getElementById)
    {
        obj = document.getElementById(id);
        if(flg =='1')
            obj.style.display = "";
        else
            obj.style.display = "none";
    }
}     
function AllowOneClick(id,msg)
{
    if (typeof(Page_ClientValidate)=='function')
    {
        if (Page_ClientValidate() == true)
        {
            return checkSubmit(id,msg);
        }
        else
        {
            return true;
        }
    }
    else
    {
        return checkSubmit(id,msg);
    }
}
function checkSubmit(id,msg)
{
    var oButton = document.getElementById(id);
    var oMsgLbl = document.getElementById('lblMsg');
    if (submitcount == 0)
    {
        if(oButton != null)
        {   
            //oButton.value = "Please wait...";
            var text = oButton.value;
            oButton.value = "";
            oButton.style.visibility = 'hidden';
            oButton.style.width = '0px';
            var oDummy = document.getElementById('btnDummy');
            oDummy.style.visibility = 'visible';
            oDummy.value = text;
            //document.forms[0].submit();
            if(oMsgLbl !=null)
            {
                oMsgLbl.innerText = msg;
            }
        }
        submitcount++; 
        return true;
    }
    else
    {
        if(oButton != null)
        {   
            oButton.value = "Please wait...";
            oButton.disabled = true;
            if(oMsgLbl !=null)
            {
                oMsgLbl.innerText = msg;
            }
        }
        return false;
    }
}
/* returns cursor pos */
function cursor_pos(el)
{
	var i = el.value.length + 1;

	if (el.createTextRange)
	{
		var cursor = document.selection.createRange().duplicate();
		while (cursor.parentElement() == el
			&& cursor.move("character", 1) == 1)
		{
			--i; 
		}
	} 
	return i;
}
//Added By Surabhi Rajan, On 03/Sep/07
function NumericOnly(e) 
{ 
    var key; 
    var keychar; 

    if (window.event) 
        key = window.event.keyCode; 
    else if (e) 
        key = e.which; 
    else 
        return true;
    
    keychar = String.fromCharCode(key); 
    keychar = keychar.toLowerCase(); 

    // control keys 
    if ((key==null) || (key==0) || (key==8) || 
                (key==9) || (key==13) || (key==27) )
        return true;
    //numbers
    else if ((("0123456789").indexOf(keychar) > -1))
            return true;
    else
        return false; 
} 
/* Validate Float values allows 'intDecimalPlaces' after decimal */
function validateFloat(e,intDecimalPlaces)
{
    var whichASC = (NS4) ? e.which : event.keyCode;
    var keyBS = 8;
	var keyDecimal = 46;
	var controlName = "";
	
	if (intDecimalPlaces == 0)
	{
		if (whichASC > 47 && whichASC < 58){		
			return true;}
		else {
			return false;}
	}
    if (NS4)
	{
		controlName = e.target.name;
		controlValue = e.target.value;
	}
	else
	{
		controlName = window.event.srcElement.getAttribute("name");
		controlValue = window.event.srcElement.getAttribute("value");
	}
	if( (((whichASC < 48) || (whichASC > 57))  && (!(whichASC == keyBS))) && (!(whichASC == keyDecimal)))
    {
		return (false);
    }
	else
	{
		if (whichASC == keyDecimal)
		{
			if (controlValue.indexOf(".") >= 0)
				{
				return false;
				}
			else
				return true;	
		}
	}
	if (intDecimalPlaces != 0)
	{
		var el = window.event.srcElement;
		
		var cur_pos = cursor_pos(el);		// return the cursor position
		if(cur_pos > (controlValue.indexOf(".") + 1))
		{		
			var txt = '';
			var foundIn = '';		
			txt = document.selection.createRange().text;					
			if (controlValue.indexOf(".") >= 0)
			{	
				if ((controlValue.length - controlValue.indexOf(".")) > intDecimalPlaces)
					if(txt=="")  return false;
			}
		}
	}
    return (true);
}
//Added By Surabhi Rajan, On 23/jun/09
function DateOnly(e) 
{ 
    var key; 
    var keychar; 

    if (window.event) 
        key = window.event.keyCode; 
    else if (e) 
        key = e.which; 
    else 
        return true;
    
    keychar = String.fromCharCode(key); 
    keychar = keychar.toLowerCase(); 

    // control keys 
    if ((key==null) || (key==0) || (key==8) || 
                (key==9) || (key==13) || (key==27) )
        return true;
    //numbers
    else if ((("0123456789/").indexOf(keychar) > -1))
            return true;
    else
        return false; 
} 
function FP_openNewWindow(w,h,nav,loc,sts,menu,scroll,resize,name,url) 
{
     var windowProperties='';
     if(nav==false)
        windowProperties+='toolbar=no,';
     else
        windowProperties+='toolbar=yes,';
     if(loc==false)
        windowProperties+='location=no,';
     else 
        windowProperties+='location=yes,';
     if(sts==false) 
        windowProperties+='status=no,';
     else
        windowProperties+='status=yes,'; 
     if(menu==false) 
        windowProperties+='menubar=no,';
     else
        windowProperties+='menubar=yes,';
     if(scroll==false)
        windowProperties+='scrollbars=no,';
     else
        windowProperties+='scrollbars=yes,';
     if(resize==false)
        windowProperties+='resizable=no,';
     else
        windowProperties+='resizable=yes,'; 
     if(w!="")
        windowProperties+='width='+w+',';
     if(h!="")
        windowProperties+='height='+h; 
     if(windowProperties!="") 
     { 
        if( windowProperties.charAt(windowProperties.length-1)==',') 
            windowProperties=windowProperties.substring(0,windowProperties.length-1);
     } 
     //alert("url : " + url +"\nname " + name + "\nwindowProperties" + windowProperties);
     window.open(url,name,windowProperties);             
}
function fnOpMidWindow(w,h,l,t,nav,loc,sts,menu,scroll,resize,name,url) 
{
     var windowProperties='';
     if(nav==false)
        windowProperties+='toolbar=no,';
     else
        windowProperties+='toolbar=yes,';
     if(loc==false)
        windowProperties+='location=no,';
     else 
        windowProperties+='location=yes,';
     if(sts==false) 
        windowProperties+='status=no,';
     else
        windowProperties+='status=yes,'; 
     if(menu==false) 
        windowProperties+='menubar=no,';
     else
        windowProperties+='menubar=yes,';
     if(scroll==false)
        windowProperties+='scrollbars=no,';
     else
        windowProperties+='scrollbars=yes,';
     if(resize==false)
        windowProperties+='resizable=no,';
     else
        windowProperties+='resizable=yes,'; 
     if(w!="")
        windowProperties+='width='+w+',';
     if(h!="")
        windowProperties+='height='+ h +',';
     if(l!="")
        windowProperties+='left='+ l +',';    
     
     if(t!="")
        windowProperties+='top='+ t +',';             
     
     if(windowProperties!="") 
     { 
        if( windowProperties.charAt(windowProperties.length-1)==',') 
            windowProperties=windowProperties.substring(0,windowProperties.length-1);
     } 
     //alert("url : " + url +"\nname " + name + "\nwindowProperties" + windowProperties);
     window.open(url,name,windowProperties);             
     //window.showModalDialog(url,name,windowProperties);             
}
function testcopy()
{
    if(document.getElementById("Checkbox1").checked == true)
    {
        document.getElementById("ctl00_ContentPlaceHolder1_txtHouseNo2").value = document.getElementById("ctl00_ContentPlaceHolder1_txtHouseNo1").value;
        document.getElementById("ctl00_ContentPlaceHolder1_txtAdd21").value = document.getElementById("ctl00_ContentPlaceHolder1_txtAdd11").value;
        document.getElementById("ctl00_ContentPlaceHolder1_txtAdd22").value = document.getElementById("ctl00_ContentPlaceHolder1_txtAdd12").value;
        document.getElementById("ctl00_ContentPlaceHolder1_txtTown2").value = document.getElementById("ctl00_ContentPlaceHolder1_txtTown1").value;
        document.getElementById("ctl00_ContentPlaceHolder1_txtCounty2").value = document.getElementById("ctl00_ContentPlaceHolder1_txtCounty1").value;
        document.getElementById("ctl00_ContentPlaceHolder1_txtPostCode2").value = document.getElementById("ctl00_ContentPlaceHolder1_txtPostCode1").value;
        document.getElementById("ctl00_ContentPlaceHolder1_txtCountry2").value = document.getElementById("ctl00_ContentPlaceHolder1_txtCountry1").value;                                                                                
    }
    else
    {
        document.getElementById("ctl00_ContentPlaceHolder1_txtHouseNo2").value = "";
        document.getElementById("ctl00_ContentPlaceHolder1_txtAdd21").value = "";
        document.getElementById("ctl00_ContentPlaceHolder1_txtAdd22").value = "";
        document.getElementById("ctl00_ContentPlaceHolder1_txtTown2").value = "";
        document.getElementById("ctl00_ContentPlaceHolder1_txtCounty2").value = "";
        document.getElementById("ctl00_ContentPlaceHolder1_txtPostCode2").value = "";
        document.getElementById("ctl00_ContentPlaceHolder1_txtCountry2").value = "";
    }
} 

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 isInteger(s) {
    return (s.toString().search(/^-?[0-9]+$/) == 0);
}

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 gtDate(sDate, sFormat)
{
    var retDate = new Date();
    switch(sFormat)
    {
        case 'dd/mm/yyyy':
        case 'DD/MM/YYYY':
        case 'dd/MM/yyyy':
            var aDate = sDate.split("/");
            var d = aDate[0];
            var m0 = aDate[1];
            var m = m0 -1 ;
            var y = aDate[2];
            retDate.setFullYear(y,m,d);
            break;
    }
    return retDate;
}
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}
