var popWindow = null;

function popwindow(url,width,height) {
	var midX = (screen.width/2)-(width/2);
	var midY = (screen.height/2)-(height/2);
	popWindow = window.open (''+[url]+'','popwin', "width="+[width]+",height="+[height]+",toolbar=no,menubar=no,scrollbars=yes,resizable=yes,top="+midY+",left="+midX+",screenX="+midX+",screenY="+midY+"");
	popWindow.focus();
}

function popwindowFixed(url,width,height) {
	var midX = (screen.width/2)-(width/2);
	var midY = (screen.height/2)-(height/2);
	popWindow = window.open (''+[url]+'','popwin', "width="+[width]+",height="+[height]+",toolbar=no,menubar=no,scrollbars=no,resizable=no,top="+midY+",left="+midX+",screenX="+midX+",screenY="+midY+"");
	popWindow.focus();
}

function breakout_of_frame_BenMoore(cartid, postbackurl)
{
 //This function breaks out of a parent frame and redirects the user to Benjamin Moore's login page.
 //The sub-site 'benjaminmoore.dvemfg.com' is not expected to be used for any other purpose other 
 //than embedded in a frame within benjamin moore's site.
 //
 //This funciton is only called from the _resources\benjaminmoore.dvemfg.com\templates\HTML\Checkout_Login.html page
 
 /*
The url will be similar to 

http://bm.com/bmpsweb/portals/bmps.portal?_nfpb=true&_pageLabel=fh_footer_hiddenPage&np=public_site%2Farticles%2Fapplication_article%2Fapp_apparelProgramLogin&
cartID=1231232&cartAction=checkout

The url has 5 parts:

1) Request URI which should be read from the Parent URL. This URI will change in dev/test/prod environments and also based on user language.
http://bm.com/bmpsweb/portals/bmps.portal

2) Request parameter _nfpb  ,which is always the following value
_nfpb=true

3) Request parameter _pageLabel .This request parameter needs to be read from the Parent URL.
   This parameter will change on different sections of the portal.
_pageLabel=fh_footer_hiddenPage

4) Request parameter np . This parameter will not change and would always be as given below
np=public_site%2Farticles%2Fapplication_article%2Fapp_apparelProgramLogin

5) CartID and CartAction, which you will be appending to the constructed URL.
cartID=1231232
cartAction=checkout
 */
  
 var RequestURI = '';
 var Request_nfpb = 'true';
 var Request_pageLabel = '';
 var Request_np = 'public_site%2Farticles%2Fapplication_article%2Fapp_apparelProgramLogin';
 var jsCartID = cartid;
 var jsCartAction = 'checkout';
 var Request_PostBackUrl = postbackurl;
 
  if (top.location != location) {
    //get RequestURI
    //alert(parent.location.hostname);
    //RequestURI = 'http://' + parent.location.hostname;
        
    //RequestURI = 'http://bm.com/bmpsweb/portals/bmps.portal';
    
    //RequestURI = 'http://dev.braveriver.com:8090/testpost.aspx'
    
    //Get Request_pageLabel
    //Request_pageLabel = queryString('_pageLabel');
    //Request_pageLabel = 'fh_footer_hiddenPage';
    //alert(RequestURI + '?_nfpb=' + Request_nfpb + '&_pageLabel=' + Request_pageLabel + '&np=' + Request_np + '&cartID=' + jsCartID + '&cartAction=' + jsCartAction);
    //top.location.href = RequestURI + '?_nfpb=' + Request_nfpb + '&_pageLabel=' + Request_pageLabel + '&np=' + Request_np + '&cartID=' + jsCartID + '&cartAction=' + jsCartAction;
  
  
    top.location.href = Request_PostBackUrl + '&cartID=' + jsCartID + '&cartAction=' + jsCartAction;
  
    //alert(Request_PostBackUrl);
  
  }
}

function PageQuery(q) {
    if(q.length > 1) this.q = q.substring(1, q.length);
    else this.q = null;
    this.keyValuePairs = new Array();
    if(q) {
        for(var i=0; i < this.q.split("&").length; i++) {
            this.keyValuePairs[i] = this.q.split("&")[i];
        }
    }

this.getKeyValuePairs = function() { return this.keyValuePairs; }
    this.getValue = function(s) {
        for(var j=0; j < this.keyValuePairs.length; j++) {
            if(this.keyValuePairs[j].split("=")[0] == s)
                return this.keyValuePairs[j].split("=")[1];
            }
        return false;
    }
        
this.getParameters = function() {
    var a = new Array(this.getLength());
        for(var j=0; j < this.keyValuePairs.length; j++) {
            a[j] = this.keyValuePairs[j].split("=")[0];
        }
    return a;
}

this.getLength = function() { return this.keyValuePairs.length; } 
}

function queryString(key){
    var page = new PageQuery(parent.location.search); 
    return unescape(page.getValue(key)); 
}

function displayItem(key){
    if(queryString(key)=='false') 
        {
            document.write("you didn't enter a ?name=value querystring item.");
        }else{
            document.write(queryString(key));
        }
    }

function setFavorite(productid, itemid, favimg) {

	var xmlHttpReq = null;
		
	if (window.XMLHttpRequest) { xmlHttpReq = new XMLHttpRequest(); }
	else if (window.ActiveXObject) { xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); }
		
	if(xmlHttpReq!=null) {

		if(itemid!="") {
			xmlHttpReq.open("GET", "/Favorites/?action=ajax+add+to+favorites&itemid="+escape(itemid), true);	
		} else {
			xmlHttpReq.open("GET", "/Favorites/?action=ajax+add+to+favorites&productid="+escape(productid), true);	
		}
		//xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlHttpReq.onreadystatechange = function() {
			if(xmlHttpReq.readyState == 4) {
				//xmlHttpReq.responseText;

				setFavIcon(favimg, xmlHttpReq.responseText);
			}
		}
		xmlHttpReq.send(null);
	}
}

function setFavIcon(favimg, httpResponse) {

	var favimgObj = document.images[favimg];
	var favimgSrc = favimgObj.src;

	if(favimgSrc.indexOf("_Y.")!=-1 && httpResponse=="delete") {
		favimgSrc = favimgSrc.replace("_Y.","_N.");
		favimgObj.src = favimgSrc;
	} else if(favimgSrc.indexOf("_N.")!=-1 && httpResponse=="add") {
		favimgSrc = favimgSrc.replace("_N.","_Y.");
		favimgObj.src = favimgSrc;
	}
}


function validateNumber(FNumber) {
	var pattern = /\D|^0/;
	if (pattern.test(FNumber.value)) {
		message = "This field accepts positive whole numbers only.\n\n"
		message += "Please try again.  Thank you."
		alert(message);
		FNumber.value = FNumber.defaultValue;
		FNumber.focus();
		return false;
	} else {
		return true;
	}
}

function validateDecimal(FNumber) {
var pattern = /[^\d\.]|\.{2,}/;
if (pattern.test(FNumber.value))
{
	message = "This field accepts positive numbers only.\n\n"
	message += "Please try again.  Thank you."
	alert(message);
	FNumber.value = FNumber.defaultValue;
	FNumber.focus();
	return false;
}
else
	return true;
}

function validateDate(FDate)
{
if (isWhitespace(FDate.value))
	return true;
if (isNaN(Date.parse(FDate.value)))
{
	text = "You have entered an invalid date.\n";
	text += "Please use the mm/dd/yyyy format.";
	alert(text);
	FDate.value = FDate.defaultValue;
	FDate.focus();
	return false;
}
else
	return true;
}

function validatePrice(FPrice)
{
if (validateDecimal(FPrice))
	FPrice.value = rectifyPrice(FPrice.value);
}

function validateEmail(FEmail)
{
FEmail.value = FEmail.value.replace(/\s/g,"");

var pattern = /^[\w-]+(\.[\w-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$/;
if (isWhitespace(FEmail.value))
	return true;
else if (pattern.test(FEmail.value))
	return true;
else
{
	message = "You have entered an invalid e-mail address.\n\n";
	message += "Please try again.  Thank you.";
	alert(message);
	FEmail.value = FEmail.defaultValue;
	FEmail.focus();
	return false;
}
}


function isURL(FValue)
{
var pattern = /^ht{2}ps?:\/{2}/i;
if (pattern.test(FValue))
	return true;
else
	return false;
}

function validateURL(FURL)
{
if (FURL.value == "")
	return true;
else if (isURL(FURL.value))
	return true;
else
{
	message = "This field must begin with http:// or https://.\n\n"
	message += "Please try again.  Thank you."
	alert(message);
	FURL.value = FURL.defaultValue;
	FURL.focus();
	return false;
}
}

function trim(FString)
{
return FString.replace(/^\s*|\s*$/g,"");
}


function isWhitespace(FValue)
{
var pattern = /^\s*$/;
if (pattern.test(FValue))
	return true;
else
	return false;
}








function checkQty(o,ordermax) {



	var val = "";
	var len = o.value.length;

	//Remove all non-numeric

    	for(i=0;i<len;i++) {
		charatA = o.value.charAt(i);
		charat0 = o.value.charCodeAt(i);
       		if(charat0 >= 48 && charat0 <= 57) { val=val+charatA; }
	}
	o.value=val;
		
	//Now check for maximum allowable order qty

	if(ordermax!="" || ordermax==0) {
		if(!isNaN(parseInt(val))) {
			if (parseInt(val) > ordermax) {
				/*
				alert('You have requested a quantity that is greater than the amount in-stock.\rYour quantity will be set to the maximum available.');
	
				if(ordermax==0) {
				o.value="";
				}
				else {
				o.value=ordermax;
				}

				*/

				if(!confirm('You have requested a quantity that is greater than the amount in-stock.\rAs a result, this item may trigger a back-order.\n\nClick OK to continue with this quantity\nClick Cancel to set your quanity to the maximum available')) {
				o.value=ordermax;
				}

				return true;
			}
			else {
			return true;
			}
		}
	}
}


function checkQtyMult(o,ordermultiple) {

	var val = "";
	var len = o.value.length;
	var setValue = 0;

	//Remove all non-numeric

    	for(i=0;i<len;i++) {
		charatA = o.value.charAt(i);
		charat0 = o.value.charCodeAt(i);
       		if(charat0 >= 48 && charat0 <= 57) { val=val+charatA; }
	}
	o.value=val;
		
	//Now check that order qty is not less than minorder

	if(ordermultiple!="") {
		if(!isNaN(parseInt(val))) {

			if (parseInt(val) < ordermultiple) {

				//o.value=ordermultiple;
			}
		}
	}
}






function hdlEnter() {
if(event.keyCode==13) { event.keyCode=9; }
}

function hdlSubmit(o) {
if(event.keyCode==13) { return false; } else { return true; }
}


var actvLastStart = 0;
var actvLastEnd = 0;
var numOfFields = 0;


function deactivateFooters(end) {

	if(end > numOfFields) {
		numOfFields = end;
	}

	var i;
	var o;

	for(i=1;i<=numOfFields;i++) {

		o = null;
		
		if(document.all) { o = document.all("MxF"+i); }
		else if(document.getElementById) { o = document.getElementById("MxF"+i); }

		if(o!=null) { o.className = "MxFAoff"; }
	}
}

function actv(start,end) {

	// to activate matrix footers

	var i;
	var o;

//alert("start:"+start+"\nend:"+end+"\nactvStart:"+actvLastStart+"\nactvEnd:"+actvLastEnd);

	//see if we changed rows
	if(start<actvLastStart || end>actvLastEnd) {

		actvLastStart = start;
		actvLastEnd = end;

		//deactivate all
		deactivateFooters(numOfFields);

		//activate current row
		for(i=start;i<=end;i++) {

			o = null;
		
			if(document.all) { o = document.all("MxF"+i); }
			else if(document.getElementById) { o = document.getElementById("MxF"+i); }

			if(o!=null) { o.className = "MxFA"; }
		}

	}
}


