﻿//This method is called on any server call as it shows a loading panel
//function Loading(show)
//{
//    var loadingImage = document.getElementById('loadingImage');
//    if (loadingImage == null)
//    {
//	    loadingImage = CreateLoadingElement();
//	    loadingImage.style.display = show ? 'block' : 'none';
//	    return true;
//	}
//	else
//	{
//	    alert('Please wait! The server is processing your request.');			    
//	    return false;
//	}
//}

function Loading(show, divID) {
    var loadingImage = document.getElementById('loadingImage');
    if (loadingImage == null) {
        loadingImage = CreateLoadingElement(divID);
        loadingImage.style.display = show ? 'block' : 'none';
        return true;
    }
    else {
        alert('Please wait! The server is processing your request.');
        return false;
    }
}

function CreateLoadingElement(divID) {
    
    var loadingImage = document.createElement('div');
    loadingImage.id = 'loadingImage';
    loadingImage.style.display = 'none';
    loadingImage.style.zIndex = 1002;
    loadingImage.style.backgroundColor = "white";
    loadingImage.style.border = "1px solid #000";
    loadingImage.style.borderColor = "black";
    loadingImage.style.borderWidth = "1px";
    loadingImage.style.top = "" + (document.body.clientHeight / 2) + "px";
    loadingImage.style.left = "" + (document.body.clientWidth / 2 - 100) + "px";
    loadingImage.style.position = 'absolute';
    loadingImage.innerHTML = "<table id=\"tbl-loading-image\"><tr><td width='5px'>&nbsp;</td><td height='40px' style='padding-top:10px; width:40px'><img src='../Images/indicator.gif' /></td><td width='10'>&nbsp;</td><td style='valign:middle; width:100px'><b>Processing...</b></td><td width='5px'>&nbsp;</td></tr></table>";

    if (divID != null) {
        document.getElementById(divID).style.border = "1px solid #000";
        document.getElementById(divID).innerHTML = loadingImage.innerHTML;
        document.getElementById("FranchiseLogin").onclick = document.getElementById("NonFranchiseLogin").onclick = document.getElementById("LogIn").onclick = "";
        document.getElementById("FranchiseLogin").style.cursor = document.getElementById("NonFranchiseLogin").style.cursor = document.getElementById("LogIn").style.cursor = "default"; 
    }else {
        document.body.appendChild(loadingImage);
    }
    
    return loadingImage;
}

//This method is called on keypress event and called the submit method of the button id passed
function doClick(buttonName,e)
{
    var key;
    if(window.event)
        key = window.event.keyCode;     //IE
    else
        key = e.which;     //firefox

    if (key == 13)
    {                
        var btn = document.getElementById(buttonName);
        if (btn != null)
        { 
            btn.click();
            event.keyCode = 0
        }
    }
}

//These methods are to format and unformat phone numbers on focus and lostfocus - wizard pages
function removeStyle(src)
{
    var src = eval(src).uniqueID;
    document.getElementById(src).value = document.getElementById(src).value.replace(/\-/g,'');
    document.getElementById(src).value = document.getElementById(src).value.replace(/\(/g,'');
    document.getElementById(src).value = document.getElementById(src).value.replace(/\)/g,'');
    document.getElementById(src).value = document.getElementById(src).value.replace(' ','');
}

function giveStyle(src)
{
    var src = eval(src).uniqueID;
    if(document.getElementById(src).value!= '' && document.getElementById(src).value != null)
    {
        if(document.getElementById(src).value.length <= 10)
        {
            var returnValue = FormatPhone(document.getElementById(src).value);
            document.getElementById(src).value = returnValue;
        }
    }
}
//

//This method allows textbox's to have either only alphabets/flot/int values
function KeyRestrict(type,e,ele)
{
	var keynum;
	var keychar;
	var check;			
	if(window.event) // IE
	{
		keynum = parseInt(e.keyCode);
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}
	keychar = String.fromCharCode(keynum);

	var ignore = (keynum==13 || keynum==8 || keynum==9 || keynum==27 || (keynum>=16 && keynum<=20) || (keynum>=35 && keynum<=46));
	if(type.match('alpha')) 
	{	
	    var isAlpha = true;
	    if(keynum>=96 && keynum<=111)
	    {
	        isAlpha = false;
	    }
		check = /[a-zA-Z\s]/;		
		return ((ignore || check.test(keychar)) && isAlpha);
	}
	else if(type.match('both'))
	{	 
	    var isTopChar = false;
	    if(keynum>=48 && keynum<=57)
	    {
	        if(e.shiftKey==1)
	        {    
	            isTopChar = true;
	        }
	    }	    
		check = /[a-zA-Z0-9\s]/;		
		return ((ignore || check.test(keychar) || (keynum>=96 && keynum<=105)) && !isTopChar);
	}
	else if(type.match('integer')) {
	
		check = /\d/;	
		return ((ignore || check.test(keychar) || (keynum>=96 && keynum<=105)) && keynum != 222 && !(e.shiftKey==1 && !ignore));
	}
	else if(type.match('float'))
	{
	    var temp = document.getElementById(ele.id+"_I").value;
	    if(temp.indexOf('.')>=0 && (keynum==110 || keynum==190))
	    {return false;}
		check = /([.\d])/;
		return ((ignore || check.test(keychar) || (keynum>=96 && keynum<=105) || (keynum==110 || keynum==190)) && !(e.shiftKey==1 && !ignore));
	}
	return false;
} 

//This method opens new dialog window
function ShowModalDialogAllModules(windowName, headerText, url, height, width)
{
    if(headerText==null || headerText=='')
    {
        headerText=windowName;
    }    
    CustomModalControl.GetWindowByName(windowName).SetHeaderText(headerText);
    CustomModalControl.ShowWindow(CustomModalControl.GetWindowByName(windowName)); 
    CustomModalControl.SetWindowContentUrl(CustomModalControl.GetWindowByName(windowName),url);                        
    CustomModalControl.SetWindowSize(CustomModalControl.GetWindowByName(windowName),height,width);
    CustomModalControl.UpdateWindowPosition(CustomModalControl.GetWindowByName(windowName));
}

//This mothod writes Notes to the grid
function CheckForEmptyNote(srcModuleID, srcNote, moduleName)
{    
    if(document.getElementById(srcNote).value == '' || document.getElementById(srcNote).value == null)
    {
        CustomAlertBoxLabel.SetText('Please write a note in the space provided and then click on Post Note');
        CustomAlertBox.Show();                
    } 
    else
    {
        if(document.getElementById(srcModuleID).value == null || document.getElementById(srcModuleID).value == '')
        {
            CustomAlertBoxLabel.SetText('Please save the '+moduleName+' Info first!');
            CustomAlertBox.Show();
        } 
        else
        {
            __doPostBack('NoteBtn','');
        }
    }
}

//This mothod writes Notes to the grid for WorkDeal module
function CheckForEmptyOfferNote(srcNote)
{    
    if(document.getElementById(srcNote).value == '' || document.getElementById(srcNote).value == null)
    {
        CustomAlertBoxLabel.SetText('Please write a note in the space provided and then click on Post Note');
        CustomAlertBox.Show();                
    } 
    else
    {
        __doPostBack('NoteBtn','');
    }
}
