﻿///<reference path="IPLCommand.js" />
// This Functions used to display or Hide the busy status.
function busy(E){
try{
    var C=getById("divStatus");
    var A=getById("tblStatus");
    var B=parseInt(document.documentElement.scrollTop+350);
    
    A.style.marginTop=B+"px";
    if(E){
        getById('divDS').style.display = "block";
        C.style.display = "block";}
    else{
        getById('divDS').style.display = "none";
        C.style.display = "none";}}
    catch(D){
    showErr("busy('"+E+"')",D)}
}


// Display error details like Function (f) which has raised the error and error object (e)
function showErr(f, e) {
    try {
        alert("Function: " + f + "\nError: " + e.message);
    } catch(e) {}
}

//This functions is used to retrieve the cookie stored.

function getCookie(strName, strDefault)
{
    if(!strDefault) { strDefault = ""; }
    var strResult = strDefault;
    try
    {
        var strSearch = strName + "=";
        var strCookie = document.cookie;
        var intStart = -1;
        var intEnd = -1;
        if(strCookie.length > 0)
        {
            intStart = strCookie.indexOf(strSearch);
            if(intStart > -1)
            {
                intStart += strSearch.length;
                intEnd = strCookie.indexOf(";", intStart);
                if(intEnd == -1)
                {
                    intEnd = strCookie.length;
                }
                strResult = unescape(strCookie.substring(intStart, intEnd));         
            }
        }
    } catch(e) {
    
        showErr( "strGetCookie('" + strName + "', '" + strDefault + "')", e);
    }
    return strResult;
}


//This functions is used to extract strName value from strdata .

function getVar(strName, strData) {
    try {
        var arrData = strData.split("|");
        var strValue = '';
        for(var i = 0; i < arrData.length; i++) {
            var arrValue = arrData[i].split("=");
            if(arrValue[0].toUpperCase() == strName.toUpperCase())
                strValue = arrValue[1];
        }
        return strValue;
    } catch(e) {
        showErr("getVar('" + strName + "')", e);
    }
}

//This functions set the strValue to cookie with name strName .

function setCookie(strName, strValue, blnPermanent)
{
    try
    {
        if(!blnPermanent) blnPermanent = false;
        var strCookie = escape(strName) + "=" + escape(strValue)
        if(blnPermanent == true)
        {
            var dtmDate = new Date ();
            dtmDate.setYear((dtmDate.getFullYear() + 1));
            strCookie += "; expires=" + dtmDate.toGMTString();
        }
        strCookie += "; path=/"
        document.cookie = strCookie;
       
        return true;
    } catch(e) {
                showErr("blnSetCookie('" + strName + "', '" + strValue + "')", e);

      
    }
    return false;
}

// Returns a object after finding the id (id). Works exactly like document.getElementById, just shortens the command and thus size of code.
function getById(id) {
    try {
    return document.getElementById(id);
    } catch(e) {
        showError("getById('" + id + "')", e);
    }
}

// This is the AJAX function which communicates with BMS-IS and executes the call back code (cc) . In case

function doTrans(a, v, t, c, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, cc, ec) {
    ec = ((!ec) ? "" : ec);
   // alert(cc);
    wu = "doTrans.aspx";
    try {        
        var ox = ((window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"));
        if(ox) {
            var p = "a=" + a + "&v=" + v + "&t=" + t + "&c=" + c + "&p1=" + p1 + "&p2=" + p2 + "&p3=" + p3 + "&p4=" + p4 + "&p5=" + p5 + "&p6=" + p6 + "&p7=" + p7 + "&p8=" + p8 + "&p9=" + p9 + "&p10=" + p10;
            ox.open("POST", wu);
            ox.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            ox.onreadystatechange = function() {                                    
                    if(ox.readyState == 4) {                                                
                        if(ox.status == 200) {    
                                
                            var x;
                            if (window.ActiveXObject) {
	                            x = new ActiveXObject("Microsoft.XMLDOM");
	                            x.async = false;	
	                            x.loadXML(ox.responseText);                                   
                            } else{ 
                                var xp =new DOMParser();
                                x = xp.parseFromString(ox.responseText, "text/xml");
                            }
                            if(x.getElementsByTagName("blnSuccess")[0].childNodes[0].nodeValue == "true") {      
                             //alert("HI");
                                if(cc != "") {
                                    if(x.getElementsByTagName("strData")[0].childNodes.length > 0) {
			                            var d = "";
			                            for(var i = 0; i < x.getElementsByTagName("strData")[0].childNodes.length; i++) {
					                        d += x.getElementsByTagName("strData")[0].childNodes[i].nodeValue;
                                        }    
                                        //alert(cc + "('" + d + "');")
                                        eval(cc + "('" + d + "');");
                                        
                                    } else {
                                        eval(cc + "();");
                                    }
                                }
                            } else {
                                eval("alert('" + x.getElementsByTagName("intExceptionEx")[0].childNodes[0].nodeValue + ": " + x.getElementsByTagName("strException")[0].childNodes[0].nodeValue + "');");
                                if(ec != "") {
                                    eval(ec + "();");
                                }
                            }
                        }
                    //} else {
                    //    alert("Technical Error !!! Please contact admin !!!");
                    }
                }
            ox.send(p);
        } else { alert("CANNOT continue !!! Please upgrade your browser or use another browser which supports AJAX !!!"); }
    } catch(e) {
        showErr("doTrans()", e);
    }
}


// Ajax function to download (content-type: text/javascript) from the server using the url (u) and then execute the JS downloaded followed by a call back code (c)
function getData(u, c) {
    try {
        var ox = ((window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"));
        if(ox) {
        
            ox.open("GET", u);
            ox.setRequestHeader("Content-Type", "text/javascript");
            ox.onreadystatechange = function() {
        
                    if(ox.readyState == 4) {
                //      alert(ox.status);
               //     alert(u);
                        if(ox.status == 200) {
                            
                            eval(ox.responseText);
                            eval(c);
                        } else {
                            alert("Technical problem !!! Please contact admin !!!");
                        }
                    }
                }
            ox.send(null);
        }
    } catch(e) {
        showErr("getData('" + u + "', '" + c + "')", e);
    }
}


// Deletes ALL the Options from the combo (c) provided and then adds a single option with innerHTML (t)

function clearOptions(c, t){
	for(var ic = c.childNodes.length - 1;ic >= 0;ic--)
    {
        ch = c.childNodes[ic];
        c.removeChild(ch);
    }
	c.appendChild(addOption(t, "", "oIBS"));
}

// Creates and Returns an Option element. It also sets the following properties of the element - innerHTML (h), value (v) and className (c)


function addOption(h, v, c) {
    var opt = document.createElement("option");
	opt.innerHTML = h;
	opt.className = c;
	opt.value = v;
	return opt;
}

function wc(str, sep, find) {
	var tempArr = str.split(sep);
	var count = 0;
	for (var i = 0; i < tempArr.length; i++) {
		if(tempArr[i].indexOf(find)>-1) {
			count++;
		}
	}
	return count;
}

//create new div with given txt (html text), c (classname), id(id to be given to new div)
//And adds the same to given object that is o

function addDiv(o, txt, c, id) {
    try {
        var d = document.createElement("div");
        d.innerHTML = txt;
        d.className = c;
        d.id = id;
        o.appendChild(d);
    }catch(e) {
        showErr("addDiv('"+o+"', '"+txt+"', '"+c+"', '"+id+"')", e);
    }
}

//following function removes the selected options from the passed select control
function remSel(sl) {
    sl = getById(sl);
	var i = sl.options[sl.selectedIndex].value;
	sl.selectedIndex = 0;
	sl.remove(i);
}


//This function is used to put the default cursor in textbox of page.

function onfocus(id){
try {
    var mytext = document.getElementById(id); 
    if (mytext!=null){mytext.focus(); }
    
    }
 catch(e)
 {
 showErr("onfocus()",e);
 }

}

// By Sanjay Dalsania - Date: 06/10/2008

function txtDisapp(o, t, e) {
    try {
        if(o.value == "" && e.type=="blur"){ o.value = t; }
        if(o.value == t && e.type=="focus"){ o.value = ""; }
    }
    catch(e) {
        showErr("txtDisapp("+o+", "+t+")", e);
    }
}


//This function is used for validation .


 function isInteger(value)  
 {  
         var re = /^\-?[0-9]+$/;  
         return re.test(value);
 }

 function checkEmail(inputvalue) 
 {
         var pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
         return pattern.test(inputvalue);
 }
    