function popupContent(cid, width, height, windowName) {
	var base_url = _PagePopupURL;
	return openPopup(base_url + '?cid=' + cid, width, height, windowName );
}

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();
}

function renderzones(zones) {
	//alert(zones);
	//var pids = "5059884,5057342,5057338,5057334";
	var pids = "";
	var trackURLs = new Array();
	for (zonekey in zones)
	{

		try
		{
		if (typeof zones[zonekey] == 'function') continue; } catch(e) { continue; };
		var separator = "";
		var zoneDivId = zones[zonekey].divId;
		var zoneDiv = document.getElementById(zoneDivId);
		//alert(zoneDivId);
		if (zoneDiv)
		{
			var recs = zones[zonekey].recs;
			var mRecs = recs.length;
			var skipPids = "";
			if (typeof maxRecs !== 'undefined') {
				mRecs = maxRecs;
			}
			if (typeof maxRecsZone2 !== 'undefined' && zoneDivId == "mybuyspagezone2") {
				mRecs = maxRecsZone2;
			}

			if (typeof dwPidsZone1 !== 'undefined') {
				skipPids = dwPidsZone1;
			}
			if (typeof dwPidsZone2 !== 'undefined' && zoneDivId == "mybuyspagezone2") {
				skipPids = dwPidsZone2;
			}
			
			//alert('(debug) skipping any pid found in: ' + skipPids);
			
			for (var i=0; i<mRecs; i++)
			{
				if (recs[i] && skipPids.indexOf(recs[i].productCode) == -1)
				{
					var productURL = recs[i].productUrl.replace('http://www.pfalztgraff.com', _HttpHomeURL);
					pids += separator + recs[i].productCode + '^' + productURL  + '^' + recs[i].trackUrl;
					separator = "|";
					//alert(recs[i].trackUrl);
					//var p = recs[i].productCode;
					//var t = recs[i].productCode + '^' + escape(recs[i].productUrl)  + '^' + escape(recs[i].trackUrl);
					//trackURLs[p] = t;
				}
			}
			var divID = '#' + zoneDivId;
			if (typeof mbSelector !== 'undefined') {
				divID = mbSelector;
			}
			
			fetchMyBuysProducts(pids,divID);
			pids = "";
			separator = "";
		}		
	}
}

function fetchMyBuysProducts(pids,mbSelector) {
	//alert(mbSelector + " will be loading pids: " + pids);
	var mini = "false";
	if (typeof isMiniCart !== 'undefined' && isMiniCart == true) {
		mini = "true";
	}

	if (pids != "") {
		var mbplURL = _MyBuysProductListURL;
		//alert(mbplURL);
		jQuery.post(mbplURL, {pids: pids, isMiniCart: mini},
			function(data) {
				//alert(data);
				if (data != null) {
					jQuery(mbSelector).html(data);
				}
			}
		);
	}
}

/*
 *  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 = ErrorPhoneNoFormattingMsgs["forms.profile.phoneNoInvalidChar"];
					break;
				case 2:
					field.innerHTML = ErrorPhoneNoFormattingMsgs["forms.profile.phoneNoInvalidLength"];
					break;
				case 3:
					field.innerHTML = ErrorPhoneNoFormattingMsgs["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";
}
