﻿// JScript File
var preid="ctl00_ContentBody_";
var prename="ctl00$ContentBody$";
var Display;
if(navigator.appName.indexOf("Microsoft")>=0)
    Display="block";
else
    Display="";
var submitValid=0;

//Prototype trim function
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); } 

//AJAX loading div switcher
function waitDisplay(showId, hideId) {
    if ((document.getElementById(showId) != null) && (document.getElementById(hideId) != null)) {
        document.getElementById(showId).style.display = "block";
        document.getElementById(hideId).style.display = "none";
    }
}

//Clear the element's value if it contains the passed text
function fnClearValue(ctrl, CompareString) {
    ControlValue = ctrl.value.toLowerCase();
    if (ControlValue.indexOf(CompareString.toLowerCase()) >= 0) {
        ctrl.value = "";
        ctrl.focus();
    }
}
//Clear a dropdownbox's selectedindex
function fnClearSelection(ctrl) {
    $get(ctrl).selectedIndex = 0;
}

//Load a default image if it is not found
function fnDefaultImg(img, folder) {
    img.src = sPath + "/Photos/" + folder + "/NoImage.gif";
}

//This fucntion is always return false inspite of browser is used
 function returnFalse()
    {
       if(navigator.appName.indexOf("Microsoft")>=0)
        {event.returnValue=false;}
       else
       {return false;}
    }
function fnChkSubmit()
{   
    if(submitValid==1)
    { submitValid=0;return returnFalse();}
    else
        return true;
}
function fnCheckEmail(ctrl)
{  if(ctrl.value=="") return;
   //if(validateEmail(document.getElementById(preid+"tbxEmail").value,1,1)==false)
   if(validateEmail(ctrl.value,1,1)==false)
   {
   	alert("Invalid email address. Please check and try again.");
   	//ctrl.focus;
   }
}
//Check that keypressed is number
function fnCheckNumber(event, obj) {
    //alert(event.keyCode);
    if ((event.keyCode >= 43 && event.keyCode <= 57)        //regular numbers
        || (event.keyCode >= 95 && event.keyCode <= 110)    //keypad numbers, or with num lock on
        || (event.keyCode == 8)                             //backspace
        || (event.keyCode == 32)                            //Space
        || (event.keyCode == 61)                            //+ (moz)
        || (event.keyCode == 187)                           //+ (IE)
        || (event.keyCode >= 37 && event.keyCode <= 40)     //arrow keys
        ) {                                                                         
            return true;
    }
    else {
        return false;
    }
}

//Check that keypressed is numeric (allow commas and dots)
function fnCheckNumeric(event, obj) {
    //alert(event.keyCode);
    if ((event.keyCode >= 43 && event.keyCode <= 57)        //regular numbers
        || (event.keyCode >= 95 && event.keyCode <= 110)       //keypad numbers, or with num lock on
        || (event.keyCode == 8)                                //backspace
        || (event.keyCode == 32)                               //Space
        || (event.keyCode == 61)                               //Plus (moz)
        || (event.keyCode == 187)                              //Plus (IE)
        || (event.keyCode >= 188 && event.keyCode <= 190)      //comma and dot
        || (event.keyCode >= 37 && event.keyCode <= 40)      //arrow keys
        ) {
        return true;
    }
    else {
        return false;
    }
}

//not implemented but useful considering inconsistencies with ascii keycodes between browsers
function fnCheckNumber1(NumStr, String) {
    for (var Idx = 0; Idx < NumStr.length; Idx++) {
        var Char = NumStr.charAt(Idx);
        var Match = false;
        for (var Idx1 = 0; Idx1 < String.length; Idx1++) {
            if (Char == String.charAt(Idx1)) Match = true;
        }
        if (!Match) return false;
    }
    return true; 
}

// This is used to check validity of Phone number Format:555-555-5555
function fnSetPhoneFormat(cntrl, lostFocus)
	{   
		var baseValue=RemoveExtraChars(cntrl.value);
		var finalValue=""
		if (baseValue=="") 
		{
			if (lostFocus==1)
				cntrl.value="";
			return;
		}
		else
		{
			finalValue="(";
			if (baseValue.length>=3)
			{
				finalValue=finalValue+baseValue.substring(0,3) + ") ";
				if (baseValue.length>=6)
				{
					finalValue=finalValue+baseValue.substring(3,6) + "-";
					if (baseValue.length>=10)
					{
						finalValue=finalValue+baseValue.substring(6,10);
					}	
					else
					{
						finalValue=finalValue+baseValue.substring(6,baseValue.length);
					}
				}	
				else
				{
					finalValue=finalValue+baseValue.substring(3,baseValue.length);
				}
			}	
			else
			{
				finalValue=finalValue+baseValue;
			}
		}
		cntrl.value=finalValue;
	}
// This is used to check validity of Phone number Format:555-555-5555
function fnSetZIPFormat(cntrl, lostFocus)
	{
		var baseValue=RemoveExtraChars(cntrl.value);
		var finalValue=""
		if (baseValue=="") 
		{
			if (lostFocus==1)
				cntrl.value="";
			return;
		}
		else
		{
			finalValue="";
			finalValue=baseValue //.substring(0,5) + "-";
			if (baseValue.length>5)
			{
				finalValue=baseValue.substring(0,5) + "-"+baseValue.substring(5,6);
				if (baseValue.length>=10)
				{
					finalValue=finalValue+baseValue.substring(6,10);
				}	
				else
				{
					finalValue=finalValue+baseValue.substring(6,baseValue.length);
				}
			}	
		}
		cntrl.value=finalValue;
	}
	
	function CommonInitializeRequestObject()
	{   var objRequest;
		try
		{
			objRequest= new ActiveXObject("Microsoft.XMLHTTP");//Try creating an XMLHTTP Object
		}
		catch(Ex)
		{
			try
			{
				objRequest= new ActiveXObject("Microsoft.XMLHTTP");//First failure, try again creating an XMLHTTP Object
			}
			catch(Ex)
			{
				objRequest= null;//Else assign null to request
			}
		}
		if(!objRequest && typeof XMLHttpRequest != 'undefined')
		{
			objRequest = new XMLHttpRequest();
		}
		return objRequest;
	}
	
//It is used to add commas in amount value.
//It return an amount value with commas format.
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
function RemoveCharsFromString(value,removeChar,replaceChar)
{ 
    var iCount;
	var returnVal="";
	if(value=="") return returnVal;
	if(value.length==null) return returnVal;
	for (iCount=0;iCount<value.length;iCount++)
	{
		if (value.charAt(iCount)==removeChar)
		    returnVal = returnVal+replaceChar;
		else	
			returnVal = returnVal+value.charAt(iCount);
	}
	return returnVal;
}
function RemoveExtraChars(value)
{
	var iCount;
	var returnVal="";
	for (iCount=0;iCount<value.length;iCount++)
	{
		if (!isNaN(value.charAt(iCount)) && value.charAt(iCount)!=" ")
			returnVal = returnVal+value.charAt(iCount);
	}
	return returnVal;
}

function PopUp(url, height, width) {
    newwindow = window.open(url, 'name', 'left=200,height=' + height + ',width=' + width + ',resizable=yes,scrollbars=yes,toolbar=no,status=no,menubar=no');
    if (window.focus) { newwindow.focus() }
    return false;
}

function validateImageFile(fileElement) {
    var OK = new Array('jpg', 'gif', 'jpeg');
    var filename = document.getElementById(fileElement).value;

    if (filename.length > 0) {
        var ext = getExt(filename);
        var fileOK = 0;

        for (i = 0; i < OK.length; i++) {
            if (OK[i] == ext) {
                fileOK = 1; // one of the file extensions found
            }
        }

        if (fileOK == 0) {
            alert("Only GIF or JPG image files are allowed for the logo.");
            return false;
        }
    }
}

function getExt(filename) {
    var dot_pos = filename.lastIndexOf(".");
    if (dot_pos == -1)
        return "";
    return filename.substr(dot_pos + 1).toLowerCase();
}

//Validate a text field to a character limit
function textCounter(field, countfield, maxlimit) {
    if (field.value.length > maxlimit) // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
    // otherwise, update 'characters left' counter
    else
        countfield.value = maxlimit - field.value.length;
}