﻿// JScript File
/* THERE ARE THE ALL COMMON JAVASCRIPT FUNCTIONS USED IN ALL APPLICATIONS, FUNCTION OF THAT FUNCTION IS GIVEN ABOVE IT
IT HAS ALSO BEEN GIVEN THE EXAMPLE THAT HOW TO CALL THIS FUNCTIONS*/

//call this function to ask the conformation to delete, pass message in parameter to write in alert box.
//i.e. OnClientClick="ConfirmDelete('Are you sure you want to delete this announcement');"

var AppGlobalPath = GetAppPath();
    function GetAppPath()
    {
        var pathArray = window.location.pathname.split( '/' );
        var newPathname = "";
            for ( i = 0; i==0; i++ )
            {
            newPathname += pathArray[i];
            newPathname += "/";
            }
        var path = window.location.protocol + "//" + window.location.host + newPathname ;
        return path
    }
    
    function FormattedAmount(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 ConfirmDelete(Message)
{
    var conf;
    conf = confirm(Message);
    if (conf == true)
    {
        return true;
    }    
    else
    {
        return false;
    }
}
//call this funtion to enter only digits... i.e onKeyPress='return NumbersOnly(event);'
//this will pass the event itself as argumet to this function
function NumbersOnly(evt)
{		    
	var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
    {
        return false;
    }
    return true;
}

//to validate email address, pass email address as string... 
//i.e ValidateEmail('abc@gmail.com') or ValidateEmail(document.getElementById("TxtEmail").value)
function ValidateEmail(EmailId)
{
        var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (filter.test(EmailId)) return true;
        else {return false;}
}

//pass the query string's name, it will return the value in that query string... i.e GetQueryStringValue('FromWhere')
//FromWhere is the query string name.. so it will return the value of that query string
function GetQueryStringValue( name )
{  
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
    var regexS = "[\\?&]"+name+"=([^&#]*)";  
    var regex = new RegExp( regexS );  
    var results = regex.exec( window.location.href );  
    if( results == null )    
        return "";  
    else    
        return results[1];
}

// TO FOLLOW UP THE USER ON HOW MANY WORDS HE HAS TYPE IN THAT CONTROL.
function GetLength(TextBoxName, LabelName)
{  
    var Total=document.getElementById(TextBoxName).value.length;
    document.getElementById(LabelName).innerHTML=Total;
}

// TO PREVENT USER TO TYPE MORE THEN MAXIMUM LENGTH OF TEXT CONTROL, MAINLY TEXT CONTROL HAVING TEXT MODE MULTILINE.
function CheckMaxLength(TextBoxName, MaxCount)
{
    var Content = document.getElementById(TextBoxName).value;
    var PrevTotal=document.getElementById(TextBoxName).value.length;
    if(PrevTotal > MaxCount-1)               
        document.getElementById(TextBoxName).value = Content.substring(0,MaxCount);
}

function GoBack()
{
    window.history.back();         
}
function SetBodyHeight()
{
    document.getElementById("ContentPart").style.height="330px"
}
function ClearHistory()
{
    //window.history.clear();
}
function ReturnFalse()
{
    return false;
}

function GetPageName(sPath)
{
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
    return sPage;
}

var xmlHttp;
function GetXmlHttpObject()
{
    //var xmlHttp=null;
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

function AddListControl(XMLDocVal,ListControl,ListTextField,ListValueField)
{
    var RecCount;
    RecCount=XMLDocVal.getElementsByTagName(ListTextField).length;
    for (i=0; i<RecCount; i++)
    {
        var Opt = document.createElement("Option");
        Opt.text = XMLDocVal.getElementsByTagName(ListTextField)[i].childNodes[0].nodeValue;
        Opt.value = XMLDocVal.getElementsByTagName(ListValueField)[i].childNodes[0].nodeValue;
        
        try
        {
            document.getElementById(ListControl).add(Opt);
        }
        catch(e)
        {
            document.getElementById(ListControl).appendChild(Opt);
        }
    }
}

function ClearListControl(ListControl)
{
    try
    {
        ListControl.options.length = 0;
    }
    catch(e)
    { 
        for (var i=ListControl.options.length-1; i>=0; i--)
        {
            ListControl.options[i] = null;
        }
        ListControl.selectedIndex = -1;
    } 
}

function leftTrim(sString) 
{
    while (sString.substring(0,1) == ' ')
    {
    sString = sString.substring(1, sString.length);
    }
    return sString;
}


function rightTrim(sString) 
{
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
    sString = sString.substring(0,sString.length-1);
    }
    return sString;
}

function CheckForBlank(val,Ele,ReEle)
{
    var ArrTxt= new String();
    ArrTxt= val.id;
    ArrTxt=ArrTxt.replace(Ele,ReEle)
    
    if( document.getElementById(ArrTxt).value == '')
    {
        return false;
    }
    else
    {
        return true;
    }
}

function Validate()
    {
     
        var count =0;
        if (document.getElementById("PGHeader_TxtBookTitle").value != '')
        {
            count = count  +1;
        }
        
        if (document.getElementById("PGHeader_TxtAuthor").value != '')
        {
            count = count  +1;
        }
        
        if (document.getElementById("PGHeader_TxtPublisher").value != '')
        {
            count = count  +1;
        }
        
        if (document.getElementById("PGHeader_TxtFromPrice").value != '')
        {
            count = count  +1;
        }
        
        if (document.getElementById("PGHeader_TxtToPrice").value != '')
        {
            count = count  +1;
        }
        
        if (document.getElementById("PGHeader_TxtISBN").value != '')
        {
            count = count  +1;
        }
        
        if (document.getElementById("PGHeader_TxtCategory").value != '')
        {
            count = count  +1;
        }
           if (document.getElementById("PGHeader_TxtFrom").value != '')
        {
            count = count  +1;
        }
           if (document.getElementById("PGHeader_TxtTo").value != '')
        {
            count = count  +1;
        }
        
        if (count ==0)
        {
            return false;
        }
        else
        {
            return true;
        }
        
    }
    function SetDefaultButton(e, buttonid)
    { 
        var evt = e ? e : window.event;
        var bt = document.getElementById(buttonid);
        if (bt)
        { 
            if (evt.keyCode == 13)
            { 
                bt.click(); 
                return false; 
            } 
        } 
}

