﻿/**
 *Common js utils
 *
 */


function getObjById( id )
{
    if (document.getElementById)
        var el = document.getElementById(id);
    else if (document.all)
        var el = document.all[id];
    else if (document.layers)
        var el = document.layers[id];
    return el;
}

function setCssClass(objName,cssClass)
{
	el = getObjById(objName);
	el.setAttribute("class", cssClass);
}

function showSpinner()
{
	setCssClass("imgSpinner", "loadingSpinnerShow");
}
function hideSpinner()
{
	setCssClass("imgSpinner", "loadingSpinnerHide");
}

function submitHandler(bttn)
{
	showSpinner();
//	bttn.disabled=true;
	return true;
}
		
	

//append date/time to URL to assure that page is refreshed
function changeURL(goURL){
	if(goURL.indexOf("?") < 0)
		goURL = goURL + "?rand=" + ((new Date()).getTime());
	else
		goURL = goURL + "&rand=" + ((new Date()).getTime());
	return goURL;
}


function gotoURL(goURL){
	window.location=changeURL(goURL);
}



function getSelectVal(field){
	if(field.selectedIndex==-1) return null;
	else return(field.options[field.selectedIndex].value)
}
function getSelectText(field){
	if(field.selectedIndex==-1) return null;
	return(field.options[field.selectedIndex].text)
}

function setSelectItem(theList,val){
	for(var i=1;i<theList.length;i++){
		if(theList.options[i].value==val){ theList.selectedIndex=i; return }
	}
	theList.selectedIndex=0
}


function printThisPage(){
	if(window.print) { //browser supports javascript print
		window.print();
	}
}

function findControl(ControlID,itemType)
{ 

	
	var ret=null;
	if(itemType=="dropdown" || itemType=="dd"){
		var aControls = document.getElementsByTagName("select");
	}else if(itemType=="img" || itemType=="image"){
		var aControls = document.getElementsByTagName("img");
	}else{
		var aControls = document.getElementsByTagName("input");
	}	
	
	if (aControls){ 
		
		for (var i=0; i< aControls.length ; i++){

			if (aControls[i].id.lastIndexOf(ControlID) == (aControls[i].id.length - ControlID.length))
				{
				ret =aControls[i];
				break;
				}
			}
		}
		
		return ret;
}


function formatCurrency(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);
}


function confirmDelete(e) {    
    return confirm("Are you sure you want to delete this item?");
}



function popWin(goURL,w,h,features){
	var featureSTR = 'width=' + w + ",height=" + h
	featureSTR += ",resizable=yes"
	if(features){
		if(features.indexOf("scrollbars")>-1 || features.indexOf("scroll")>-1) { featureSTR+=",scrollbars=yes"  }
		if(features.indexOf("location")>-1 || features.indexOf("address")>-1){ featureSTR+=",location=yes"  }
		if(features.indexOf("menubar")>-1 || features.indexOf("menu")>-1)	{ featureSTR+=",menubar=yes"  }
		if(features.indexOf("status")>-1 || features.indexOf("statusbar")>-1)	{ featureSTR+=",status=yes"  }
		if(features.indexOf("toolbar")>-1 || features.indexOf("tool")>-1)	{ featureSTR+=",toolbar=yes"  }
		if(features.indexOf("all")>-1)		{ featureSTR+=",scrollbars=yes,location=yes,menubar=yes,status=yes,toolbar=yes"  }
	}
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	//center window
	featureSTR+=",top="+wint+",left="+winl
	
	openPopupWindow(goURL,'popper',featureSTR)
}

var gNewWindow=null
function openPopupWindow(theURL,winName,features) {
	//stamp random num to end of URL to assure it's refreshed
	theURL=changeURL(theURL); 

	if (!gNewWindow || gNewWindow.closed) {
		gNewWindow = window.open(theURL,winName,features);
	} else {
	// window already exists, so close it and reload new window
	if(gNewWindow){ gNewWindow.close(); }
		gNewWindow = window.open(theURL,winName,features);
	}
	
	gNewWindow.focus();
}




var keyStr = "ABCDEFGHIJKLMNOP" +
               "QRSTUVWXYZabcdef" +
               "ghijklmnopqrstuv" +
               "wxyz0123456789+/" +
               "=";

function encode64(input) {
    input = escape(input);
    var output = "";
    var chr1, chr2, chr3 = "";
    var enc1, enc2, enc3, enc4 = "";
    var i = 0;

    do {
        chr1 = input.charCodeAt(i++);
        chr2 = input.charCodeAt(i++);
        chr3 = input.charCodeAt(i++);

        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 63;

        if (isNaN(chr2)) {
            enc3 = enc4 = 64;
        } else if (isNaN(chr3)) {
            enc4 = 64;
        }

        output = output +
           keyStr.charAt(enc1) +
           keyStr.charAt(enc2) +
           keyStr.charAt(enc3) +
           keyStr.charAt(enc4);
        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";
    } while (i < input.length);

    return output;
}

function decode64(input) {
    var output = "";
    var chr1, chr2, chr3 = "";
    var enc1, enc2, enc3, enc4 = "";
    var i = 0;

    // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
    var base64test = /[^A-Za-z0-9\+\/\=]/g;
    if (base64test.exec(input)) {
        alert("There were invalid base64 characters in the input text.\n" +
              "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" +
              "Expect errors in decoding.");
    }
    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

    do {
        enc1 = keyStr.indexOf(input.charAt(i++));
        enc2 = keyStr.indexOf(input.charAt(i++));
        enc3 = keyStr.indexOf(input.charAt(i++));
        enc4 = keyStr.indexOf(input.charAt(i++));

        chr1 = (enc1 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;

        output = output + String.fromCharCode(chr1);

        if (enc3 != 64) {
            output = output + String.fromCharCode(chr2);
        }
        if (enc4 != 64) {
            output = output + String.fromCharCode(chr3);
        }

        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";

    } while (i < input.length);

    return unescape(output);
}

