﻿/*
 * GdirectCommon.js
 */

/*----------------------------------------------------------------------------------
 * Called to force-load the js. This is a hack to try to ensure that the code is 
 * loaded in Safari as a debugging hack. It doesn't seem to be loading 
 * for new registrants which causes the Custom Validator's call for
 * phone number formatting to NOT get done properly (i.e. validatePhoneNumberFormat
 * doesn't get called).
 * But validatePhoneNumberForma works in IE so it's probably not a loading thing...
 *--------------------------------------------------------------------------------*/
function doNothing() {
	var x = 1;
	x = x + 1;
}

/*
 * Taken from http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript
 */
/*
Number.prototype.formatMoney = function(c, d, t) {
	var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
	return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};
*/

/*----------------------------------------------------------------------------------
 *--------------------------------------------------------------------------------*/
function ApplyCurrencyFormat(oSrc, args) {
    var val = args.Value;
    if( val == "" ) {
		return true;
    }
    var stripped = val.replace('$', '');

	var i = parseFloat( stripped );
    if( isNaN(i) ) {
		i = 0.00;
	}
    var minus = '';
    if(i < 0) {
		minus = '-';
	}
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if(s.indexOf('.') < 0) {
		s += '.00';
	}
    if(s.indexOf('.') == (s.length - 2)) {
		s += '0';
	}
    s = minus + s;
    var formattedVal = s;
    //return s;


//	if (isNaN(parseInt(stripped))) {
//        args.IsValid = false;
//        oSrc.innerText = "* Use 123.45 format";
//        retVal = false;
//    }    
//    else {
	if( s.length > 1 ) {
		var controlToValidate = null;
        try {
		    var attributeName = "controltovalidate";
		    controlToValidate = document.getElementById(oSrc.controltovalidate);
		    controlToValidate.value = formattedVal;
		}
		catch (err) {
		}
		return true;
    }
}

/*----------------------------------------------------------------------------------
 *--------------------------------------------------------------------------------*/
function applyEmailAddrFormat(oSrc, args) {
    var val = args.Value;
    if( val == "" ) {
		return true;
    }
    else {
		var lcaseEmailAddr = val.toLowerCase();
		var controlToValidate = null;
        try {
		    var attributeName = "controltovalidate";
		    controlToValidate = document.getElementById(oSrc.controltovalidate);
		    controlToValidate.value = lcaseEmailAddr;
		}
		catch (err) {
		}
		return true;
    }
}

/*----------------------------------------------------------------------------------
 *--------------------------------------------------------------------------------*/
function applyCityAddrFormat(oSrc, args) {
    var val = args.Value;
    if( val == "" ) {
		return true;
    }
    else {
		var lcaseCityName = val.toLowerCase();
		var rule1 = (lcaseCityName.indexOf("greater") != -1);
		var rule2 = (lcaseCityName.indexOf("metro") != -1);
		var rule3 = (lcaseCityName.indexOf("area") != -1);
		var rule4 = (lcaseCityName.indexOf("region") != -1);
		if( rule1 || rule2 || rule3 || rule4 ) {
			alert("Attention: Selecting a full address will provide the best presentation of your business on the map.");
		}
		return true;
    }
}

/*----------------------------------------------------------------------------------
 *--------------------------------------------------------------------------------*/
function validatePhoneNumberFormat(oSrc, args) {
    var val = args.Value;
    var retVal = true;

    var stripped = val.replace(/[\(\)\.\-\ ]/g, '');

    if (val == "") {
        args.IsValid = false;
        oSrc.innerText = "* Please enter a phone number";
        retVal = false;
    }
    else if (isNaN(parseInt(stripped))) {
        args.IsValid = false;
        oSrc.innerText = "* Use (XXX) XXX-XXXX format";
        retVal = false;
    }
    else if (!(stripped.length == 10)) {
        args.IsValid = false;
        oSrc.innerText = "* Use (XXX) XXX-XXXX format";
        retVal = false;
    }
    else {
        // retVal is true for this entire case
        
        var formattedPhoneNumber = "(";
        formattedPhoneNumber += stripped.substr(0, 3);
        formattedPhoneNumber += ") "
        formattedPhoneNumber += stripped.substr(3, 3);
        formattedPhoneNumber += "-"
        formattedPhoneNumber += stripped.substr(6, 4);

        //var controlToValidate = oSrc.attributes("controltovalidate").value;
        //document.getElementById(controlToValidate).value = formattedPhoneNumber;
        
		var controlToValidate = null;
        try {
		    var attributeName = "controltovalidate";
		    controlToValidate = oSrc.attributes(attributeName).value;
		    document.getElementById(controlToValidate).value = formattedPhoneNumber;
		}
		catch (err) {
		    controlToValidate = document.getElementById(oSrc.controltovalidate);
		    controlToValidate.value = formattedPhoneNumber;
		}

    }
    return retVal;
}

/*----------------------------------------------------------------------------------
 *--------------------------------------------------------------------------------*/
function ValidateURLFormat(oSrc, args) {
    var val = args.Value;
    var retVal = true;

    var stripped = val.replace('http://', '');
    stripped = stripped.replace('https://', '');

    if (val == "") {
        args.IsValid = false;
        oSrc.innerText = "* Do not include 'http://' in the URL";
        retVal = false;
    }
    else {
        // retVal is true for this entire case        
        var formattedURL = stripped.toLowerCase();
        
		var controlToValidate = null;
        try {
		    var attributeName = "controltovalidate";
		    controlToValidate = oSrc.attributes(attributeName).value;
		    document.getElementById(controlToValidate).value = formattedURL;
		}
		catch (err) {
		    controlToValidate = document.getElementById(oSrc.controltovalidate);
		    controlToValidate.value = formattedURL;
		}

    }
    return retVal;
}

function showPanel(w, h, dim, src, html, panelTitle) {
	/*
	w = Width in pixels
	h = height in pixels
	dim = dimmer value 0 = no dim, 1 = 100%
	src = page source displayed in an iFrame.  If blank, then no iframe
	html = html to display in pop-up
	btnClass = Class name to use to render the buttons.
	
	*/

	var winW = clientWidth() + scrollLeft();
	var winH = clientHeight() + scrollTop();
	var dlgLeft = ((winW - w) / 2) - 50;
	var dlgTop = 20;    //(winH - h) / 2;
	document.getElementById('popupPanel').style.left = dlgLeft + "px";
	document.getElementById('popupPanel').style.top = dlgTop + "px";

	// --- Show the image upload panel
	if (src == "") {
		// --- No SRC means we should have some HTML to display
		document.getElementById('popupPanel').innerHTML = html;
		document.getElementById('popupPanel').style.padding = "20px";
		document.getElementById('popupPanelBG').style.opacity = dim;
		document.getElementById('popupPanelBG').style.filter = 'alpha(opacity=' + dim * 100 + ')';
		document.getElementById('popupPanel').style.height = h + "px";
		document.getElementById('popupPanel').style.width = w + "px";
		document.getElementById('popupPanel').style.position = "fixed";

		document.getElementById('popupPanelBG').style.display = "block";
		document.getElementById('popupPanel').style.display = "block";

	} else {
		// --- Having a SRC value means that we should include an Iframe component
		var iFrameHeight = h - 49;
		iframeHTML = '<br /><iframe frameborder="0" scrolling="yes" class="popupPanelFrame" id="popupPanelFrame" height="' + iFrameHeight + '" width="100%"></iframe>';
		//iframeHTML = '<br /><iframe frameborder="0" scrolling="yes" class="popupPanelFrame" id="popupPanelFrame" height="100%" width="100%"></iframe>';
		iframeHTML = iframeHTML + '<br />';
		document.getElementById('popupPanelBody').innerHTML = iframeHTML;
		document.getElementById('popupPanelFrame').src = src;

		document.getElementById('popupPanelBG').style.opacity = dim;
		document.getElementById('popupPanelBG').style.filter = 'alpha(opacity=' + dim * 100 + ')';
		document.getElementById('popupPanel').style.height = h + "px";
		document.getElementById('popupPanel').style.width = w + "px";
		document.getElementById('popupPanelHeader').style.width = w - 117 + "px";
		document.getElementById('popupPanelHeader').innerHTML = panelTitle;
		document.getElementById('popupPanelCloseButton').style.width = "116px";
		document.getElementById('popupPanelCloseButton').innerHTML = "Close";
		document.getElementById('popupPanel').style.position = "fixed";

		document.getElementById('popupPanelBG').style.display = "block";
		document.getElementById('popupPanel').style.display = "block";

	}
}

function hidePanel() {
	document.getElementById('popupPanelBG').style.display = "none";
	document.getElementById('popupPanel').style.display = "none";
}

/*----------------------------------------------------------------------------------
 * "client" with these methods is the browser
 *--------------------------------------------------------------------------------*/
function clientWidth() {
    return filterResults(
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}

/*----------------------------------------------------------------------------------
 *--------------------------------------------------------------------------------*/
function clientHeight() {
    return filterResults(
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}

/*----------------------------------------------------------------------------------
 *--------------------------------------------------------------------------------*/
function scrollLeft() {
    return filterResults(
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}

/*----------------------------------------------------------------------------------
 *--------------------------------------------------------------------------------*/
function scrollTop() {
    return filterResults(
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

/*----------------------------------------------------------------------------------
 *--------------------------------------------------------------------------------*/
function filterResults(n_win, n_docElement, n_body) {
    var n_result = n_win ? n_win : 0;
    if (n_docElement && (!n_result || (n_result > n_docElement)))
        n_result = n_docElement;
    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

