// common.js
// javascript functions

_objectMoving = "";
_objectMovingIF = "";

function updateButtonClass(amtValue)
{
  // alert((amtValue);
}


function openShoppingBag(totAmount,MinAmount,addtoUrl,lang)
{

	if($('shopBag'))
	{
		document.body.removeChild($('shopBag'));
	}

	mainDiv = document.createElement('DIV');
	mainDiv.id = 'shopBag';
	mainDiv.className = 'shoppingBagContainer';

	overlay = document.createElement('DIV');
	overlay.className = 'overlayBG';
	mainDiv.appendChild(overlay);

	iframeEmpty = document.createElement('IFRAME');
	iframeEmpty.className = 'shoppingBagFrameInvisible';
	iframeEmpty.frameBorder = 0;
	iframeEmpty.name = 'iEmpty';
	iframeEmpty.id = 'iShoppingEmpty';
    iframeEmpty.src = '';

	mainDiv.appendChild(iframeEmpty);

	opaque = document.createElement('DIV');
	opaque.className = 'opaqueBG';
	mainDiv.appendChild(opaque);

	closeX = document.createElement('INPUT');
	closeX.id = 'windowClose';
	closeX.className = 'shopBagCloseBtn';
	closeX.type = 'submit';
	closeX.value = 'x';
	closeX.onclick = closeShopBag;
	opaque.appendChild(closeX);

	h2Header = document.createElement('H2');
	h2Header.innerHTML = translate.shoppingBag;
	opaque.appendChild(h2Header);

	minimumOrder = document.createElement('DIV');
	minimumOrder.id = 'minOrder';
	minimumOrder.className = 'minimumOrder';
	opaque.appendChild(minimumOrder);
	minimumOrder.innerHTML = '';

	orderSumBar = document.createElement('DIV');
	orderSumBar.id = 'orderSumBar';


	tab1 = document.createElement('DIV');
	tab1.className = 'prodTitle';
	tab1.innerHTML = translate.product;

	tab2 = document.createElement('DIV');
	tab2.className = 'brandTitle';
	tab2.innerHTML = translate.style;

	tab3 = document.createElement('DIV');
	tab3.className = 'colorTitle';
	tab3.innerHTML = translate.color;

	tab4 = document.createElement('DIV');
	tab4.className = 'sizeTitle';
	tab4.innerHTML = translate.size;

	tab5 = document.createElement('DIV');
	tab5.className = 'priceTitle';
	tab5.innerHTML = translate.price;

	tab6 = document.createElement('DIV');
	tab6.className = 'qtyTitle';
	tab6.innerHTML = translate.qty;

	tab7 = document.createElement('DIV');
	tab7.className = 'totalTitle';
	tab7.innerHTML = translate.total;

	tab8 = document.createElement('DIV');
	tab8.className = 'removeTitle';
	tab8.innerHTML = '';

	orderSumBar.appendChild(tab1);
	orderSumBar.appendChild(tab2);
	orderSumBar.appendChild(tab3);
	orderSumBar.appendChild(tab4);
	orderSumBar.appendChild(tab5);
	orderSumBar.appendChild(tab6);
	orderSumBar.appendChild(tab7);
	orderSumBar.appendChild(tab8);
	opaque.appendChild(orderSumBar);

	iframe = document.createElement('IFRAME');
	iframe.className = 'shoppingBagFrame';
	iframe.frameBorder = 0;
	iframe.name = 'iShopping';
	iframe.id = 'iShopping';
	iframe.src = '/frontEndComponents/specificComponents/b2c/shoppingCart/shoppingCartDetails.cfm?'+''+addtoUrl;

	opaque.appendChild(iframe);

	buttonHolder = document.createElement('DIV');
	buttonHolder.className = 'buttonHolder clearfix';

	kshopping = document.createElement('BUTTON');
	kshopping.className = 'shoppingCartKeepShopping shoppingCartKeepShopping'+ lang;
	kshopping.name = 'keepShopping keepShopping'+ lang;
	kshopping.id = 'keepShopping keepShopping'+ lang;
	kshopping.innerHTML = translate.keepShopping + ' &raquo;';
	kshopping.onclick = closeShopBag;
	   if(totAmount >=MinAmount)
	   {
	     pclass='shoppingCartCheckout shoppingCartCheckout'+ lang;
	   }
	   else
	   {
	     pclass='shoppingCartCheckoutGrey shoppingCartCheckoutGrey'+ lang;
	   }

	pcheckOut = document.createElement('BUTTON');
	//pcheckOut.type = 'submit';
	pcheckOut.className = pclass;
	pcheckOut.name = 'checkoutB';
	pcheckOut.id = 'checkoutB';
	pcheckOut.innerHTML = translate.checkout + ' &raquo;';

	buttonHolder.appendChild(pcheckOut);
	buttonHolder.appendChild(kshopping);

	opaque.appendChild(buttonHolder);
	document.body.appendChild(mainDiv);

}

function closeShopBag() {
	document.body.removeChild($('shopBag'));
}


function openBrandPopup(theurl,brandid,brandurl,lang)
{

 if($('brandPopup'))
 {
  document.body.removeChild($('brandPopup'));
 }

 mainDiv = document.createElement('DIV');
 mainDiv.id = 'brandPopup';
 mainDiv.className = 'brandNav';

 closeX = document.createElement('INPUT');
 closeX.id = 'windowClose';
 closeX.className = 'shopBagCloseBtn';
 closeX.type = 'submit';
 closeX.value = 'x';
 closeX.onclick = closeBrandPopup;
 mainDiv.appendChild(closeX);

 iframe = document.createElement('IFRAME');
 iframe.className = 'shoppingBagFrame';
 iframe.frameBorder = 0;
 iframe.name = 'iBrandPopup';
 iframe.id = 'iBrandPopup';
iframe.src = theurl+'?brandid='+brandid+"&lang="+lang+"&brandurl="+brandurl;

 mainDiv.appendChild(iframe);
 document.body.appendChild(mainDiv);
}

function closeBrandPopup() {
 document.body.removeChild($('brandPopup'));
}

function DisplaySearchBox(id)
	{
	  //alert(id);
	    //alert($(id));
		$(id).style.display = $(id).style.display == 'none'?'block':'none';
	}


function changeClassName(theitem, NewclassName) {
  if((document.getElementById(theitem).className != NewclassName)) {
    document.getElementById(theitem).className = NewclassName;
  } else {
    document.getElementById(theitem).className = 'clearfix';
  }
}

function toggleOneItem(theitem) {
  if((document.getElementById(theitem).style.display == 'block') || (document.getElementById(theitem).style.display == -1))
  	{
    document.getElementById(theitem).style.display = 'none';
    document.getElementById('invisibleFrame').style.display = 'none';
    }
  else
    document.getElementById(theitem).style.display = 'block';
}


function openWindow(theURL,w,h) {
  posx=(screen.availWidth-w)/2;
  posy=(screen.availHeight-h)/2;
  window.open(theURL,'popup','toolbar=no,location=no,status=no,menubar=no,scrollbars=auto,resizable=yes'+',width='+w+',height='+h+',left='+posx+',top='+posy);
}

function getPageScroll(){

	var yScroll, arrayPageScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll)
	return arrayPageScroll;
}

// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){

	var xScroll, yScroll, pageHeight, pageWidth, arrayPageSize;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

function findClassProperties(style,rule)
{
	styles = document.styleSheets;
	for(x=0;x<styles.length;x++)
	{
		href = styles[x].href;
	    if (href != null){
		    if(href.indexOf(style))
			{
				rules = (styles[x].rules)?styles[x].rules:styles[x].cssRules;

				for(zz=0;zz<rules.length;zz++)
				{
					temp1 = rules[zz];
					temp2 = rule;

					if(temp1.type!=3)
					{
						if(temp1.selectorText.toUpperCase() == rule.toUpperCase())
						{
							return rules[zz];
						}

					}
				}
			}
		}
	}
}

_cssClass = "";

function showAlert(obj) {
    url = (!obj.url)?'':obj.url;
	message = (!obj.message)?'':obj.message;
	targetAlert = (!obj.target)?'alertbox':obj.target;
	cssClass = (!obj.cssClass)?'alertbox':obj.cssClass;
	closeBText = (!obj.closeBText)?'X':obj.closeBText;

	checkObject = $(targetAlert);
	checkObjectIF = $(targetAlert+'IF');

	if(checkObject!=null)
	{
		killAlertBox();
	}

	_objectMoving = document.createElement('DIV');

	_objectMoving.id = targetAlert;
	_objectMoving.className = cssClass;
	_objectMoving.style.display = 'block';

	if(document.all&&(navigator.userAgent.indexOf('MSIE 7.0')<0))
	{
		_objectMovingIF = document.createElement('IFRAME');
		_objectMovingIF.id = targetAlert+'IF';
		_objectMovingIF.className = cssClass;
		_objectMovingIF.style.display = 'block';
		_objectMovingIF.style.border = "0";
		_objectMovingIF.scrolling = "no";
		_objectMovingIF.frameborder = false;
	}

	_cssClass = '.'+cssClass;


	DWREngine._execute(_cfGenericFunctionsLocation, null, 'getText',escape(message),url,closeBText,'1',callText_Result);

}

function showPopUp(obj) {

	url = (!obj.url)?'':obj.url;
	message = (!obj.message)?'':obj.message;
	targetAlert = (!obj.target)?'alertbox':obj.target;
	cssClass = (!obj.cssClass)?'alertbox':obj.cssClass;
	closeBText = (!obj.closeBText)?'x':obj.closeBText;
	checkObject = $(targetAlert);
	checkObjectIF = $(targetAlert+'IF');

	if(checkObject!=null)
	{
		killAlertBox();
	}

	_objectMoving = document.createElement('DIV');

	_objectMoving.id = targetAlert;
	_objectMoving.className = cssClass;
	_objectMoving.style.display = 'block';

	if(document.all&&(navigator.userAgent.indexOf('MSIE 7.0')<0))
	{
		_objectMovingIF = document.createElement('IFRAME');
		_objectMovingIF.id = targetAlert+'IF';
		_objectMovingIF.className = cssClass;
		_objectMovingIF.style.display = 'block';
		_objectMovingIF.style.border = "0";
		_objectMovingIF.scrolling = "no";
		_objectMovingIF.frameborder = false;
	}

	_cssClass = '.'+cssClass;

	DWREngine._execute(_cfCatLevelsLocation, null, 'getProductInfo',message,closeBText,callText_Result);
}




function callText_Result(result)
{

	try{
		result = decodeURIComponent(result);
	}catch(err){
		;
	}
	//alert(result);
	_objectMoving.innerHTML = result;
	//alert(_objectMoving.innerHTML);
	arrayPageScroll = getPageScroll();
	arrayPageSize = getPageSize();

	var target_temp = arrayPageSize[3];

	box = findClassProperties('mainStyles.css',_cssClass).style;

	boxHeight = parseInt(box.height);
	boxWidth = parseInt(box.width);

	targetY = ((arrayPageScroll[1])+(arrayPageSize[3]/2))-(boxHeight/2)-90;
	targetX = (arrayPageSize[2]/2)-(boxWidth/2);

	_objectMoving.style.position = 'absolute';
	_objectMoving.style.top = targetY+'px';
	_objectMoving.style.left = targetX+'px';

	if(document.all&&(navigator.userAgent.indexOf('MSIE 7.0')<0))
	{
		_objectMovingIF.src = siteUrl + "/frontEndComponents/specificComponents/b2c/presentationLayer/blank.html";
		_objectMovingIF.zIndex = parseInt(box.zIndex)-1;
		_objectMovingIF.style.filter = "alpha(opacity=0)";
		_objectMovingIF.style.position = 'absolute';
		_objectMovingIF.style.top = targetY+'px';
		_objectMovingIF.style.left = targetX+'px';
		_objectMovingIF.style.padding = box.padding;
		_objectMovingIF.frameBorder = false;
		_objectMovingIF.style.width = parseInt(box.width);
		_objectMovingIF.style.height = parseInt(box.height)+25;
		_objectMovingIF.style.width = parseInt(box.width)+11;
		document.body.appendChild(_objectMovingIF);
	}

	document.body.appendChild(_objectMoving);
}

function killAlertBox()
{

	document.body.removeChild(_objectMoving);
	if(document.all&&(navigator.userAgent.indexOf('MSIE 7.0')<0))
	{
			document.body.removeChild(_objectMovingIF);
	}

}

function killAlertBoxAndBacktoResult()
{
	killAlertBox();
	window.location=document.getElementById("backtoCategoryResultURL").href;
}

function init()
{
	DWREngine._errorHandler =  errorHandler;
}

init();

//used for interactpopup
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
