// -----------------------------------------------------------------------------------------------------
// ArchetypeClothing.js
//
// Dynamic Content Utilities
//
// Author        : Thomas E. George
// Creation Date : July 02, 2006
//
// Notes         : assumes inclusion of ArchetypeClothingIncl.js prior to this file
//
// History       : 07/02/2006 - TEG - Initial Implementation
// -----------------------------------------------------------------------------------------------------

var eMailAddress = null;
var docImages    = null;

var emfmsg       = "subscribe"; //"enter your e-mail here for offers and photos";
var eMADefColor  = "gray";
var eMAColor     = "cyan";
var emfblurred   = true;

var _popupwin    = null;

function process_error(msg)
{
	if((msg != null) && (msg.length > 0))
	{
	    if(debugModeFlag)
	    {
			alert(msg);
		}
	}
}

function onClicked()
{
	emailfieldclicked();
	emailfieldfocused();
	showsubscribe();
	emailfieldcursorchange('black');
}

function onSelected()
{
	emailfieldselected();
	emailfieldfocused();
	showsubscribe();
	emailfieldcursorchange('black');
	focusControl(eMailAddress);
}

function onBlurred()
{
	emailfieldblurred();
	emailfieldcursorchange('transparent');
}

function showsubscribe()
{
	try
	{
		if(document.getElementById)
		{
			oRow  = document.getElementById('submitrow');
			oCell = document.getElementById('submittd');
			
			backBtn = document.getElementById('goBackBtn');
		}
		else if(document.all)
		{
			oRow  = document.all.submitrow;
			oCell = document.all.submittd;
			
			backBtn = document.all.goBackBtn;
		}
		
		var btnInnerHTML = "<input name=\"emailsub\" type=\"submit\" value=\"click here to SUBSCRIBE\" style=\"FONT-FAMILY: Century Gothic; FONT-SIZE: 12; FONT-WEIGHT: regular\" size=\"30\" class=\"thinput\" />";
		
		if(oCell.innerHTML.length == 0)
		{
			if(backBtn != null)
			{
				//backBtn.setAttribute("visible", "false");
				//backBtn.style.visiblity = "false";
				backBtn.disabled = true;
			}
			
			var newCell = document.createElement("TD");
			newCell.setAttribute("id", "submittd");
			newCell.setAttribute("align", "center");
			//newCell.setAttribute("style", "VISIBILITY: visible"); // hidden
			newCell.innerHTML = btnInnerHTML;
			oRow.replaceChild(newCell, oCell);
		}
		
		//oCell.getElementByTagName("EMailRegistryReg").Visible = true;
		//oCell.visible = true;
	}
	catch(e)
	{
	    if(e.description.length != 0) { process_error("ArchetypeClothing.js:showsubscribe() : Exception caught [" + e.description + "]"); }
	}
	finally
	{
	}
}

function setemailfieldvalue(color, msg)
{
	try
	{
		eMailAddress.style.color = color;
		eMailAddress.value       = msg;
	}
	catch(e)
	{
	    if(e.description.length != 0) { process_error("ArchetypeClothing.js:setmailfieldvalue() : Exception caught [" + e.description + "]"); }
	}
	finally
	{
	}
}

function emailfieldvalue()
{
	try
	{
		if(emfblurred)
		{
			setemailfieldvalue(eMADefColor, emfmsg);
		}
	}
	catch(e)
	{
	    if(e.description.length != 0) { process_error("ArchetypeClothing.js:emailfieldvalue() : Exception caught [" + e.description + "]"); }
	}
	finally
	{
	}
}

function emailsubscribebtn()
{
	try
	{
		if(!emfblurred)
		{
			if(isvalidemailaddr(eMailAddress.value))
			{
				showsubscribe();
			}
		}
	}
	catch(e)
	{
	    if(e.description.length != 0) { process_error("ArchetypeClothing.js:emailsubscribebtn() : Exception caught [" + e.description + "]"); }
	}
	finally
	{
	}
}

function emailfieldfocused()
{
	try
	{
		emfblurred = false;
	}
	catch(e)
	{
	    if(e.description.length != 0) { process_error("ArchetypeClothing.js:emailfieldfocused() : Exception caught [" + e.description + "]"); }
	}
	finally
	{
	}
}

function emailfieldblurred()
{
	try
	{
		emfblurred = true;
	}
	catch(e)
	{
	    if(e.description.length != 0) { process_error("ArchetypeClothing.js:emailfieldblurred() : Exception caught [" + e.description + "]"); }
	}
	finally
	{
	}
}

function focusControl(control)
{
	try
	{
		if(control != null)
		{
			control.focus();
		}
	}
	catch(e)
	{
		if(e.description.length != 0) { process_error("ArchetypeClothing.js:focusControl(" + control + ") : '" + e.name + "' Exception caught [" + e.description + "]"); }
	}
	finally
	{
	}
}

function emailfieldclicked()
{
	try
	{
	    if(eMailAddress.value.length == 0)
	    {
			setemailfieldvalue(eMAColor, ""); //(eMADefColor, emfmsg);
		}
		else
		{
			if(!isvalidemailaddr(eMailAddress.value))
			{
				setemailfieldvalue(eMAColor, "");
			}
		}
	}
	catch(e)
	{
	    if(e.description.length != 0) { process_error("ArchetypeClothing.js:emailfieldclicked() : Exception caught [" + e.description + "]"); }
	}
	finally
	{
	}
}

function emailfieldselected()
{
	try
	{
		eMailAddress.style.color = eMAColor;
	}
	catch(e)
	{
	    if(e.description.length != 0) { process_error("ArchetypeClothing.js:emailfieldselected() : Exception caught [" + e.description + "]"); }
	}
	finally
	{
	}
}

function isvalidemailaddr(emailaddr)
{
    var rc = false;
    
	try
	{
	    if(emailaddr != null && emailaddr.length > 0)
	    {
			var RE = "^[\\w\\.-]+@[\\w-]+\\.[\\w\\.-]+$";
			
			var result = emailaddr.match(RE);
			
			if(result != null)
			{
				rc = true;
			}
		}
	}
	catch(e)
	{
	    if(e.description.length != 0) { process_error("ArchetypeClothing.js:isvalidemailaddr() : Exception caught [" + e.description + "]"); }
	}
	finally
	{
	}
	
	return(rc);
}

function isnumeric(val, addlChars)
{
    var rc = false;
    
	try
	{
	    if(val != null && val.length > 0)
	    {
			//var RE = "/^[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?$/";
			//
			//var result = val.match(RE);
			//
			//if(result != null)
			//{
			//	rc = true;
			//}
			
			if(addlChars == null)
			{
			    addlChars = "";
			}
			
			var bRes          = true;
            var strValidChars = "0123456789" + addlChars; //.-";

            for(var i = 0; i < val.length; i++)
            {
                if(strValidChars.indexOf(val.charAt(i)) == -1)
                {
                    bRes = false;
                    break;
                }
            }
            
            rc = bRes;
        }
	}
	catch(e)
	{
	    if(e.description.length != 0) { process_error("ArchetypeClothing.js:isnumeric() : Exception caught [" + e.description + "]"); }
	}
	finally
	{
	}
    
    return(rc); 
}

function isvalidccnumber(val)
{
    var rc = false;
    
	try
	{
	    // AMEX has 15 digits, the rest have 16
	    if((val != null) && ((val.length >= 15) && (val.length <= 16)) && isnumeric(val, null))
	    {
			rc = true;
		}
	}
	catch(e)
	{
	    if(e.description.length != 0) { process_error("ArchetypeClothing.js:isvalidccnumber() : Exception caught [" + e.description + "]"); }
	}
	finally
	{
	}
	
	return(rc);
}

function isvalidcvv2number(val)
{
    var rc = false;
    
	try
	{
	    if((val != null) && ((val.length >= 3) && (val.length <= 4)) && isnumeric(val, null))
	    {
			rc = true;
		}
	}
	catch(e)
	{
	    if(e.description.length != 0) { process_error("ArchetypeClothing.js:isvalidcvv2number() : Exception caught [" + e.description + "]"); }
	}
	finally
	{
	}
	
	return(rc);
}

function isvalidzipcode(val)
{
    var rc = false;
    
	try
	{
	    if((val != null) && ((val.length == 5) || (val.length == 10)) && isnumeric(val, "-"))
	    {
			rc = true;
		}
	}
	catch(e)
	{
	    if(e.description.length != 0) { process_error("ArchetypeClothing.js:isvalidzipcode() : Exception caught [" + e.description + "]"); }
	}
	finally
	{
	}
	
	return(rc);
}

function timedadvance(runImmediate)
{
	try
	{
		if(runImmediate)
		{
			emailfieldvalue();
		}
		
		setTimeout('emailfieldvalue()', 6000);
	}
	catch(e)
	{
	    if(e.description.length != 0) { process_error("ArchetypeClothing.js:timedadvance() : Exception caught [" + e.description + "]"); }
	}
	finally
	{
	}
}

function emailfieldcursorchange(color)
{
	try
	{
		//alert("emailfieldcursorchange() called");
		//eMailAddress.style.cursor="text";
		if(emfblurred)
		{
			eMailAddress.style.backgroundColor = color;
		}
	}
	catch(e)
	{
	    if(e.description.length != 0) { process_error("ArchetypeClothing.js:emailfieldcursorchange(" + color + ") : Exception caught [" + e.description + "]"); }
	}
	finally
	{
	}
}

function goBack()
{
	try
	{
	    var url = document.referrer;
	    
	    if(url.length > 0)
	    {
			window.location = url;
	    }
	}
	catch(e)
	{
		process_error("ArchetypeClothing.js:goBack() : '" + e.name + "' Exception caught [" + e.description + "]");
	}
	finally
	{
	}
}

function bodyOnLoad()
{
	try
	{
		// Before the error event handler can take effect, we have to register it for this window.
		//self.onerror = report_error;
		self.onerror = supress_errors;

		eMailAddress = document.getElementsByName("eMailAddress")[0];
		
		if(navigator.appName.indexOf("Netscape") != -1)
		{
			// Netscape navigator in use
			//+++ fix anomalous behavior with text field properties +++
			try
			{
				eMailAddress.style.width           = "286px"; //eMailAddress.style.width * 2;
				eMailAddress.style.height          = "23px";
				eMailAddress.style.fontFamily      = "Century Gothic";
				eMailAddress.style.color           = "Cyan";
				eMailAddress.style.backgroundColor = "Transparent";
				eMailAddress.style.borderColor     = "White";
				eMailAddress.style.borderStyle     = "Solid";
				eMailAddress.style.borderWidth     = "1px";
				eMailAddress.style.fontSize        = "10pt";
				//eMailAddress.style.filter          = "revealTrans(duration=2,transition=3)";
				
				//+++TEST CODE+++
				//process_error("Why are you using Netscape Navigator?");
				// The following line of code purposely causes an error as a test.
				//alert(no_such_variable);
			}
			catch(e)
			{
				if(e.description.length != 0) { process_error("ArchetypeClothing.js:bodyOnLoad() : Netscape block : Exception caught [" + e.description + "]"); }
			}
			finally
			{
			}
		}
		else
		{
			//+++TEST CODE+++
			//process_error("Ahhhhhh... Internet Explorer!");
			// The following line of code purposely causes an error as a test.
			//alert(no_such_variable);
		}
		
		//EMailRegistryReg = document.getElementsByTagName("EMailRegistryReg")[0];
		
		timedadvance(false);
	}
	catch(e)
	{
	    if(e.description.length != 0) { process_error("ArchetypeClothing.js:bodyOnLoad() : Exception caught [" + e.description + "]"); }
	}
	finally
	{
	}
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Payment Form validation
function onPayButtonHit()
{
    alert("onPayButtonHit()");
    //process_error(msg);
    
	var ZipCode = document.getElementsByName("BuyerZipCodeTextBox")[0];

    if(ZipCode.value.length > 0)
    {
        alert("Zip code good.");
        
	    //var PayButton = document.getElementsByName("PayButton")[0];
    }
}

//<a href="popupcontent.html" onclick="return jsPopup(this, 'whatsthis')">what's this?</a>
function jsPopup(link, windowname, iTop, iLeft, iWidth, iHeight)
{
    var rc = true;
    
    try
    {
        if(window.focus)
        {
            var href = null;
            
            if(typeof(link) == 'string')
            {
                href = link;
            }
            else
            {
                href = link.href;
            }
            
            var wstyle = 'top=' + iTop + ',left=' + iLeft + ',width=' + iWidth + ',height=' + iHeight + ',location=no,scrollbars=no,menubar=no,resizable=no,status=no,toolbar=no';
            
            _popupwin = window.open(href, windowname, wstyle);
            
            _popupwin.focus();
            
            rc = false
        }
	}
	catch(e)
	{
	    if(e.description.length != 0)
	    {
	        process_error("ArchetypeClothing.js:jsPopup() : Exception caught [" + e.description + "]");
	    }
	    else
	    {
	        process_error("ArchetypeClothing.js:jsPopup() : ERROR - Exception occurred --- Cannot process.");
	    }
	}
	finally
	{
	}
    
    return rc;
}

function jsPopupClose()
{
    try
    {
        if(_popupwin && (_popupwin != null) && !_popupwin.closed) 
        {
            _popupwin.close();
        }
	}
	catch(e)
	{
	    if(e.description.length != 0)
	    {
	        process_error("ArchetypeClothing.js:jsPopupClose() : Exception caught [" + e.description + "]");
	    }
	    else
	    {
	        process_error("ArchetypeClothing.js:jsPopupClose() : ERROR - Exception occurred --- Cannot process.");
	    }
	}
	finally
	{
	}
}

