﻿ var ISIE;
if (navigator.appName=="Microsoft Internet Explorer")
    ISIE = true;
else
    ISIE = false;

function ShowDialog(navigateurl,width,height)
{
    var w = width, h = height;
    
    
   if (document.all || document.layers)
   {
    w = screen.availWidth;
    h = screen.availHeight;
   }
   
    var leftPos,topPos;    
    var popW = width, popH = height;
    if(document.all)    
    {leftPos = (w-popW)/2; topPos = (h-popH)/2;}
    else
    {leftPos = w/2; topPos = h/2;}
    
    window.open(navigateurl ,"popup","width=" + popW + ",height=" + popH + ",top=" + topPos +",left=" + leftPos);         
}
//get value is used to get control value like textbox,lable
function GetValue(objCtrl)
{
    // based on the Type and Tag Name set proper value and use proper  property    
    var ctrlType = objCtrl.type;
    var ctrlTagName = objCtrl.tagName;
    if(ctrlType != undefined)
    {
        ctrlType=ctrlType.toLowerCase();
        switch(ctrlType)
        {
        case "text":
        case "hidden":                    
            return objCtrl.value;
            break;
        case "checkbox":
        case "radio":            
            return objCtrl.checked;
            break;
        default:
            alert('SetValue-General.js-Invalid Type '+ ctrlType);
            break;
        }
    }
    else
    {
        ctrlTagName=ctrlTagName.toLowerCase();
        switch(ctrlTagName)
        {
        case "div":
        case "span":                    
        case "label":
            return objCtrl.innerHTML;
            break;    
        default:
            alert('SetValue-General.js-Invalid Tag '+ ctrlTagName);
            break;
        }
    }
}

//---This function will return any value with 2 decimal places and
//---replace with 1 or 2 zeros if needed
function xCents(value){
	value="" + Math.round(value*Math.pow(10,2))/Math.pow(10,2);
	dec1 = value.substring(value.length-3, value.length-2);
	dec2 = value.substring(value.length-2, value.length-1);
	if (dec1 != '.') { // adds trailing zeroes if necessary
	if (dec2 == '.') value += "0";
	else value += ".00";
	}
	return value;
}
//SetValue function is used to set value in a control ex : textbox ,lable etc.
function SetValue(objCtrl,value)
{
    // based on the Type and Tag Name set proper value and use proper  property    
    
    var ctrlType = objCtrl.type;
    var ctrlTagName = objCtrl.tagName;
    if(ctrlType != undefined)
    {
        ctrlType=ctrlType.toLowerCase();
        switch(ctrlType)
        {
        case "text":
        case "hidden":                    
            objCtrl.value=value;
            break;
        case "checkbox":
        case "radio":
            if(value == '')
            {
                objCtrl.checked = false;
            }            
            else
            {
                objCtrl.checked = value;
            }
            break;
        default:
            alert('SetValue-General.js-Invalid Type '+ ctrlType);
            break;
        }
    }
    else
    {
        ctrlTagName=ctrlTagName.toLowerCase();
        switch(ctrlTagName)
        {
        case "div":
        case "span":                    
        case "label":
            objCtrl.innerHTML =value;
            break;    
        default:
            alert('SetValue-General.js-Invalid Tag '+ ctrlTagName);
            break;
        }
    }
}
//check zipcode enter is valid IsValidZipCode is used zipcode should be of five digits or zero length.
function IsValidZipCode(objZipCode)
{
    var zip = objZipCode.value;
     if(zip.length == 5 || zip.length == 0)
        return true;
     else
        return false;
}
//check phone number enter is valid . it should be of 10 digits or zero length.
function IsValidPhone(objPhone1,objPhone2,objPhone3)
{
        var Phone=objPhone1.value + objPhone2.value + objPhone3.value;
       // phone=phone.trim();
        if(Phone.length == 10 || Phone.length == 0)
            return true;
        else
            return false;
}
//function is used to check maximum length of input characters in textbox
function CheckMaxLength(e,txt,max) 
{
    if (txt.value.length >= max && e.keyCode != 8)
    {txt.value = txt.value.substring(0,max);return false;}
    else{return true;}
}
//set focus on destination control if source control length =3
function SetFocus(objSource,objDestination)
{
   
   if(window.event) // IE
   {
         keynum = event.keyCode;
         if(keynum == 9 && objSource.value.length == 3)
         {
               objSource.focus();
               return false;
        
          }   
         else if(objSource.value.length == 3)
         {
            objDestination.focus();
         }
   }
   else 
   {
         if(objSource.value.length == 3)
         {
            objDestination.focus();
         }
   }
   
}
//this function make ex : value 2300 to 2,300 format.
function formatCurrencyStr(num)
{
    
    if (num == null) { num = ""; }
            num = num.toString().replace(/\$|\,/g, '');
            if (isNaN(num))
                num = "0";
            sign = (num == (num = Math.abs(num)));
            num = Math.floor(num * 100 + 0.50000000001);
            cents = num % 100;
            num = Math.floor(num / 100).toString();
            if (cents < 10)
                cents = "0" + cents;
            for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
                num = num.substring(0, num.length - (4 * i + 3)) + ',' +
    num.substring(num.length - (4 * i + 3));
            return (((sign) ? '' : '-') + num + '.' + cents);
}

//remove , from a value like 2,300 = 2300
function RemoveCommaFromStr(StrValue)
{
    return StrValue.replace(/,/g,"");
}

// check for valid ajax response and session
function IsValidAJAXResponse(AJAXResponse)
 {
    if (AJAXResponse.error == null){
        
        if(AJAXResponse.value.indexOf('error') != -1){
            alert(AJAXResponse.value);        
            return false;
        }
        
        if (AJAXResponse.value == null){
            alert('AJAXResponse is NULL');
            return false;
        }
        
        return true;
    }
    else { alert(AJAXResponse.error); return false; }
}

function PrintElement(objElement, arrHideShowObjects) {
    var ArrStyle = new Array();
    if (arrHideShowObjects != undefined && arrHideShowObjects != null) {
        for (var i = 0; i < arrHideShowObjects.length; i++) {
            ArrStyle.push(document.getElementById(arrHideShowObjects[i]).style.display);
            document.getElementById(arrHideShowObjects[i]).style.display = 'none';
        }
    }    

    var objClone = objElement.cloneNode(true);
    document.body.appendChild(objClone);
    document.body.style.backgroundColor = '#FFFFFF';
    window.print();
    document.body.removeChild(objClone);
    document.body.style.backgroundColor = '#cccccc';

    if (arrHideShowObjects != undefined && arrHideShowObjects != null) {
        for (var i = 0; i < arrHideShowObjects.length; i++) {

            document.getElementById(arrHideShowObjects[i]).style.display = ArrStyle[i];
        }
    }

    return;
}