function whyPhonePopup(){
	popupContent("WHY_PHONE", 600, 400, "PhonePopUp" ).focus();
} 
function whyeMailPopup(){
	popupContent( "WHY_EMAIL", 600, 400, "EmailPopUp" ).focus();
} 
function whyCVNPopup(){
	popupContent( "WHY_CVN", 600, 400, "CVNPopUp" ).focus();
}

/*
 *  This set of functions are validating and formatting the 
 * input for phone number fields
 */

// error status of the last beautification
var lastError;
/*
 * This function takes a string representing a phone number,
 * strips away all that is not a number and formats the 
 * phone number like this: XXX-XXX-XXXX OOO
 * The XXX part is the wanted part by Pfaltzgraff; the OOO are extra digits 
 * The value passed in may consist of digits or these characters "() +-./".
 * Any other character will cause an error.
 * @param:
 *    rawPhone:  String; the number to be formatted
 *    required:  boolean; if set to true this field may not be empty
 * @return
 *    the function returns the beautified string
 *
 * it also modifies the global variable <CODE>lastError</CODE> holding
 * the error code of this beautification.
 *   0 - no error
 *   1 - other characters other than the numbers and allowed values
 *   2 - there were not 10 numbers in the string
 *   3 - the fied is marked as required but it is empty
 */
function beautifyPhoneNo( rawPhone, required )
{
	lastPhoneNoFormatError = 0;
	var beauty = "";
	var c;
	// strip away unwanted characters
	for ( i = 0; i < rawPhone.length; i++ )
	{
		c = rawPhone.charAt( i );
		if ( c >= '0'  && c <= '9' )
		{
			beauty += c;
			continue;
		}
		// check for valid seperators
		if ( "() +-./".indexOf( c ) >= 0 )
		{
			continue;
		}
		
		// this shouldn't be implemented as a THROW thingy.
		// but the FF3 at least acted funny.  It took some considerable long time
		// for the FF3 to recover from the catch.  Thus return it wih a flag - for now...
		// illegal charaters
		// alert( "illegal character ( " + c + ")" );
		// throw "illegal character:" + c;
		lastPhoneNoFormatError = 1;
		return rawPhone;
	}
	// beautify the phone number
	if ( beauty.length > 0 )
	{
		// remove the prefix 1 for US long distance
		if ( beauty.charAt( 0 ) == '1' && beauty.length > 10 )
		{
			beauty = beauty.substring( 1 );
		}
		if ( beauty.length != 10 )
		{
			lastPhoneNoFormatError = 2;
		}
		// put in format delimeter.  Group: 3-3-4 n
		if ( beauty.length > 3)
		{
			beauty = beauty.substring( 0, 3 ) + "-" + beauty.substring( 3 );
			if ( beauty.length > 7)
			{
				beauty = beauty.substring( 0, 7 ) + "-" + beauty.substring( 7 );
				if ( beauty.length > 12)
				{
					beauty = beauty.substring( 0, 12 ) + " " + beauty.substring( 12 );
				}
			}
		}
	}
	else
	{
		if ( required )
		{
			lastPhoneNoFormatError = 3;
		}
	}
	return beauty;
}

/*
 * error treatment, detailing the HTML persentation
 * Setting the label to read and showing error texts
 */
function handleErrorPhoneNoFormatting( fieldName )
{
	// alert( "Got exception " + raw );
	field = document.getElementById( "Label_" + fieldName );
	if ( field != null )
	{
		if ( lastPhoneNoFormatError == 0 )
		{
			field.className = "";
		}
		else
		{
			field.className = "warning";
		}
	}
	field = document.getElementById( "Error_" + fieldName );
	if ( field != null )
	{
		if ( lastPhoneNoFormatError == 0 )
		{
			field.style.display = "none";
		}
		else
		{
			field.style.display = "";
			switch ( lastPhoneNoFormatError )
			{
				case 1:
					field.innerHTML=PhoneMessages["forms.profile.phoneNoInvalidChar"];
					break;
				case 2:
					field.innerHTML=PhoneMessages["forms.profile.phoneNoInvalidLength"];
					break;
				case 3:
					field.innerHTML=PhoneMessages["forms.profile.phoneNoEnterNumber"];
					break;
			}
		}
	}
	lastPhoneNoFormatError = 0;
}

/*
 * This function resets all currently showing error messages for this field
 */
function clearPhoneNoError( fieldName )
{
	lastPhoneNoFormatError = 0;
	handleErrorPhoneNoFormatting( fieldName )
}

/*
 * for simplified usage.  Just call this method with the name of the 
 * formfield.  
 *	pdict.CurrentForms.profile.login.phone.htmlName
 *  pdict.CurrentForms.profile.login.phone.mandatory
 */ 
function customizePhoneNoField( fieldName, required )
{
	var field = document.getElementsByName( fieldName );
	if ( required == null )
		alert( "Please update your source and provide the field 'required' for the phone number" );
		
	if ( field != null && field.length > 0 )
	{
		field = field[0];
		// alert ( field );
		field.onblur = function()
		{
			var raw = document.getElementsByName( fieldName )[0].value;
			try
			{
				raw = beautifyPhoneNo( raw, required == "true" );
				document.getElementsByName( fieldName )[0].value = raw;
				handleErrorPhoneNoFormatting( fieldName );
			}
			catch ( e )
			{
				// for some odd reason the try/throw/catch thing lasts forever using FF3
				// thus I flagged the return value
				handleErrorPhoneNoFormatting( fieldName );
			}
		}
		field.value = beautifyPhoneNo( field.value, required == "true" );
		// field.onblur();
	}
}

/**
 * This function resets all errors shown by an IS Inputfield.
 * As a requirement the field needs to have the label and the error text.
 * only when both can be found, the error wil be resettet.
 */ 
function resetErrorsOnForm( formId )
{
	var form = document.getElementById( formId );
	if ( form != null )
	{
		var field;
		var fieldName;
		var label;
		var errorTxt;
		for ( i = 0; i < form.elements.length; i ++ )
		{
			
			field = form.elements[i];
			fieldName = field.name;
			
			if ( fieldName != null )
			{
				if ( (label=document.getElementById( "Label_" + fieldName )) != null )
				{
					if ( (errorTxt=document.getElementById( "Error_" + fieldName )) != null )
					{
						label.className = "";
						errorTxt.style.display = "none";
					}
				}
			}
		} 
	}
}

/*
 *  This set of functions are related to red tag clearance layer
 */
var mousein = true;
var curClearanceLayer = null; //holds currently visible clearance div (see pricing.isml)
	
function hideClearanceLayer() {
	if (curClearanceLayer != null)
	{
		curClearanceLayer.style.display = "none";
	}
}
function setClearanceLayerPosition(obj, id) {
	if (curClearanceLayer != null)
	{
		curClearanceLayer.style.display = "none";
	}
	var layerId = "clearance_layer" + id;
	var curleft = 0;
	var curtop = 0;
	for (var tmp = obj; tmp != null; tmp = tmp.offsetParent)
	{
		curleft += tmp.offsetLeft;
		curtop += tmp.offsetTop;
	}
	curleft += 25;
	curtop -= 7;
	curClearanceLayer = document.getElementById(layerId); 
	curClearanceLayer.style.left = curleft+"px";
	curClearanceLayer.style.top = curtop+"px";
	if (curClearanceLayer.style.display == "block") 
		curClearanceLayer.style.display = "none"; 
	else 
		curClearanceLayer.style.display = "block";
}

/* script to reset popup layer sizing; when dynamically resizing 
the popup layer, the orginal dimensions are lost & results in a 
layer too small to show the 'added to cart' layer contents; script will 
get original dimensions and then apply them when called */
jQuery(document).ready(function(){
	var $cart = jQuery(".cartquickview"),  
	cartSettings = {
		"height":$cart.height()+"px", 
		"width":$cart.width()+"px", 
		"marginLeft":$cart.css("margin-left")};
	setLayerSize = function(){
		var t = setTimeout(function(){
			jQuery(".cartquickview").css({
				"height":cartSettings.height, 
				"width":cartSettings.width, 
				"margin-left":cartSettings.marginLeft}
			)}, 
		1500);
	};
});
