

function uncheckAllCheckbox(ckeckboxId,formId){ 
	var checkbox = document.getElementById(ckeckboxId);
	var frm = document.getElementById(formId); 
	
	var elemnets = frm.elements; 
	for(i = 0;i < elemnets.length; i++) {
		// and check if it is a checkbox
		if(elemnets[i].type == "checkbox" && elemnets[i] != checkbox) { 
    		if(checkbox.checked){
    			elemnets[i].checked = false;
    		} 
		}
	}
}
function uncheckUnsubscribe(ckeckboxId,unsubscribeId){ 
	var checkbox = document.getElementById(ckeckboxId);
	var unsubscribe = document.getElementById(unsubscribeId); 
 
	if(checkbox.type == "checkbox" && checkbox.checked) {  
		unsubscribe.checked = false; 	  
	}
	 
}

function ClosePaymentDivs(){
	DivClose('CreditCard');
	DivClose('Paypal');
	DivClose('Solo');
	DivClose('ErrorDiv');
	DivClose('RTBT');
	DivClose('BT');
	DivClose('konbini');
	DivClose('CashOnDelivery');
	DivClose('boletos');
}

function selectPaymentOption(cookieValue,tableId){
	ClosePaymentDivs(); 
	createCookie('paymentMethod',cookieValue);
	DivOpen(tableId);
	
	// if we have "shippingOptionsDiv" tag, means we need to refresh the shipping methods
	var shippingOptionsDiv=document.getElementById("shippingOptionsDiv");
	if(shippingOptionsDiv)
		reloadShippingOptions(tableId);
}

function reloadShippingOptions(tableId){
	var queryStr="?paymentType="+tableId;
	getAjaxText("ajax/checkoutShippingOptions.jsp"+queryStr,refreshShippingOPtions,error);
}

function refreshShippingOPtions(data){

	// parse the returned data, and removed the redundant <form> tag
	var formReg=/<form action="#">/;
	var sphtml=data.replace(formReg,"");
	formReg=/<\/form>/;
	sphtml=sphtml.replace(formReg,"");

	var shippingOptionsDiv=document.getElementById("shippingOptionsDiv");
	shippingOptionsDiv.innerHTML=sphtml;
	
	// update the shipping cost on the side bar as the chosen shipping option may be different than previous one 
	var shippingOptRadio=document.getElementsByName("/atg/commerce/order/ShoppingCartModifier.hardgoodShippingGroup.shippingMethod");
	if(shippingOptRadio){
		for(i=0;i<shippingOptRadio.length;i++){
			if(shippingOptRadio[i].checked){
				updateShippingMethodFunc(shippingOptRadio[i].value);
				break;
			}
		}	
	}	
}


function DivClose(num) {
var div = document.getElementById(num);
    if(div != null)
	   div.style.display="none";
} 

function DivOpen(num) {
var div = document.getElementById(num);
    if(div != null)
	   div.style.display="block";
} 

function MenuFull() {
var Full = document.getElementById("Full");
var Upgrade = document.getElementById("Upgrade");
	Upgrade.style.display="none";
	Full.style.display="block";
} 

function MenuUpgrade() {
var Full = document.getElementById("Full");
var Upgrade = document.getElementById("Upgrade");
	Full.style.display="none";
	Upgrade.style.display="block";
} 

function MenuChange(num) {
var menu = document.getElementById(num);
var tagPages = document.getElementById('tagPages');
var tagPagesNodes = tagPages.childNodes;
var taglist = new Array();
var x = 0;
	for(var t=0; t<tagPagesNodes.length; t++){
		
		if(tagPagesNodes[t].nodeName == 'DIV' && tagPagesNodes[t].id == num){
			taglist[x] = tagPagesNodes[t];
			tagPagesNodes[t].style.display = "block";
			x++;
			
		}else if(tagPagesNodes[t].nodeName == 'DIV'){
			taglist[x] = tagPagesNodes[t];
			tagPagesNodes[t].style.display = "none";
			x++;
		}
		
	}
	
	var Tag = document.getElementById('Tag');
	var TagNodes = Tag.childNodes;
	for(var g=0; g<TagNodes.length; g++){
		if(TagNodes[g].nodeName == 'DIV'){
			TagNodes[g].style.display = "none";
		}
	}
	
	for(var c=0; c<taglist.length; c++){
		if(taglist[c].nodeName == 'DIV' && taglist[c].id == num){
			var taglistId2 = taglist[c].id;
			var tagOpen = document.getElementById(taglistId2+'Tag2');
			tagOpen.style.display = "block";
		}else if(taglist[c].nodeName == 'DIV'){
			var taglistId = taglist[c].id;
			var tagClose = document.getElementById(taglistId+'Tag');
			tagClose.style.display = "block";
		}
	}
} 

function OpenMenu(num) {
	if(navigator.appName == "Netscape"){
		if(num != 'Swimwear'){
			var level2 = document.getElementById(num);
			var level2Node = level2.lastChild.previousSibling;
			level2Node.style.display="block";
		}
		var level1 = document.getElementById(num);
		level1.setAttribute('class','level1 clicked');
		var level1Link = document.getElementById("root"+num);
		level1Link.firstChild.style.color = "#FFF";
	}
	else if(navigator.appName == "Microsoft Internet Explorer"){
		if(num != 'Swimwear'){
			var level2 = document.getElementById(num);
			var level2Node = level2.lastChild;
			level2Node.style.display="block";
		}
		var level1 = document.getElementById(num);
		level1.className ="level1 clicked";
		var level1Link = document.getElementById("root"+num);
		level1Link.firstChild.style.color = "#FFF";
	}
} 

function CloseMenu(num) {
	if(navigator.appName == "Netscape"){
		if(num != 'Swimwear'){
			var level2 = document.getElementById(num);
			var level2Node = level2.lastChild.previousSibling;
			level2Node.style.display="none";
		}
		var level1 = document.getElementById(num);
		level1.setAttribute('class','level1');
		var level1Link = document.getElementById("root"+num);
		level1Link.firstChild.style.color = "#C7DEEF";
	}
	else if(navigator.appName == "Microsoft Internet Explorer"){
		if(num != 'Swimwear'){
			var level2 = document.getElementById(num);
			var level2Node = level2.lastChild;
			level2Node.style.display="none";
		}
		var level1 = document.getElementById(num);
		level1.className ="level1";
		var level1Link = document.getElementById("root"+num);
		level1Link.firstChild.style.color = "#C7DEEF";
	}
} 

function showWindow(url, name, width, height) {
	width += 32;
	height += 96;
	try {
		var props = "width=" + width + ",height=" + height+",scrollbars=yes";
		if (typeof(screen.width)!='undefined') {
			props += ",top=" + ((screen.height-height)/2);
			props += ",left=" + ((screen.width-width)/2);
		}
		var win = window.open(url,'dialogWindow',props);
		if (parseInt(navigator.appVersion) >= 4) { 
			win.window.focus();
		}
	} catch(err) {
		alert(err.message);
	}
}

var count = true;
function tag() {
	var tagUpgradea= document.getElementById('tagUpgradea');
	var tagFulla = document.getElementById('tagFulla');
	var tagUpgradeaNodes = tagUpgradea.childNodes;
	var tagFullaNodes = tagFulla.childNodes;
	if(count == true){
		
		tagFulla.style.background="url(../img/tag/tagBG2.jpg) top center no-repeat";
		tagUpgradea.style.background="url(../img/tag/tagBG.jpg) top center no-repeat";
		tagUpgradea.innerHTML= '<span id="tagUpgradeb" style="">Upgrade</span>';
		tagFulla.innerHTML= '<span><a href="javascript:MenuFull();tag();" id="tagFullb">Full</a></span>';
		count = false;
	}else if(count == false){
		tagFulla.style.background="url(../img/tag/tagBG.jpg) top center no-repeat";
		tagUpgradea.style.background="url(../img/tag/tagBG2.jpg) top center no-repeat";
		tagFulla.innerHTML= '<span id="tagFullb" style="">Full</span>';
		tagUpgradea.innerHTML= '<span><a href="javascript:MenuUpgrade();tag();" id="tagUpgradeb">Upgrade</a></span>';
		count = true;
	}
} 

function showDialog(url, width, height){

	width=800;
	if(typeof(window.showModalDialog)=='object'){
		/*
		* In IE 7 the XMLHttpRequest is a Native object, so use it to determine IE version
		*/
		if(typeof(XMLHttpRequest)!='object'){
			height = height + 50;
			width = width + 20;
		}
		var features = "center:1;dialogHeight:" + height + "px;dialogWidth:" + width + "px;scroll:yes";
		window.showModalDialog(url,'',features);
	}else{
		var props = "width=" + width + ",height=" + height+",location=0";
		if(typeof(screen.width)!='undefined'){
			props += ",top=" + ((screen.height-height)/2);
			props += ",left=" + ((screen.width-width)/2);
			props += ",scrollbars=1";
			props += ",status=0,toolbar=0,titlebar=0,menubar=0,location=0";
		}
		var win = window.open(url,'dialogWindow',props);
		if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		//date.setTime(date.getTime()+(5*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function nukecookie(cookiename)
{
	var dt = new Date();
	document.cookie = cookiename + "=0;expires=" + dt.toGMTString() + ";" + ";";
	return;
}

var check = true;
function DivDisplay(num) {
	var div = document.getElementById(num);

	if(check == true){
		div.style.display="block";
		check = false;
	}else if(check == false){
		div.style.display="none";
		check = true;
	}

}


function getWindowScroll() {
    var T, L, W, H;            
    var browserLanguage= detectBrowserLanguage();
    if (browserLanguage.browser != 'Netscape') {
      T = document.documentElement.scrollTop;
      L = document.documentElement.scrollLeft;
      W = document.documentElement.scrollWidth;
      H = document.documentElement.scrollHeight;
    } 
    else {
      var w = window;
      with (w.document) {
        if (w.document.documentElement && documentElement.scrollTop) {
          T = documentElement.scrollTop;
          L = documentElement.scrollLeft;
        } else if (w.document.body) {
          T = body.scrollTop;
          L = body.scrollLeft;
        }
        if (w.innerWidth) {
          W = w.innerWidth;
          H = w.innerHeight;
        } else if (w.document.documentElement && documentElement.clientWidth) {
          W = documentElement.clientWidth;
          H = documentElement.clientHeight;
        } else {
          W = body.offsetWidth;
          H = body.offsetHeight
        }
      }
    }
	return { top: T, left: L, width: W, height: H };
}

var t;

function slideDown() {
	 var windowScroll = parent.getWindowScroll(); 
	 var popupTable = parent.window.document.getElementById('popupTableShipping');
	 popupTable.style.margin = windowScroll.top + "px auto auto auto";
	 t=setTimeout("slideDown()",100);
}

function detectBrowserLanguage(){
var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);
return {browser: browser ,version: version};
}

function getPageSize() {          
    var windowWidth, windowHeight;
    var pageHeight, pageWidth;
	var browserLanguage= detectBrowserLanguage();
    if (browserLanguage.browser != 'Netscape') {
      windowWidth = document.documentElement.clientWidth;
      windowHeight = document.documentElement.clientHeight;  
      pageWidth = document.documentElement.scrollWidth;
      pageHeight = document.documentElement.scrollHeight;   
    } 
    else {
      var xScroll, yScroll;
      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;
      }


      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;
      }
    }             
    return {pageWidth: pageWidth ,pageHeight: pageHeight , windowWidth: windowWidth, windowHeight: windowHeight};
}

var r;
var countNum = 0;

function opacity(num){
	var div = document.getElementById(num);
	countNum = countNum +0.1;
	if(countNum <=1){
		div.style.opacity = countNum;
	}
	r=setTimeout("opacity('"+num+"')",50);
}

function resetOpacity(){
	stopCount(r);
	countNum = 0;
	}


function openPopup(num, Id) {
		var num = num;
		var popupDiv = document.getElementById(num);
		var popupTable = document.getElementById('popupTable');
		var popupBG = document.getElementById('black');
		var pageSize = getPageSize();
		popupBG.style.height = pageSize.pageHeight + "px";
		var popupId = popupDiv.childNodes;
		popupTable.style.height = pageSize.windowHeight + "px";
		popupBG.style.display="block";

		// the passed id is null from cartSummary.jsp ... do the judgement first
		if(Id != 'noId'){
			var popupIdd = document.getElementById(Id);
			popupIdd.style.display="block";
		}	
		
		var titleDiv = popupDiv.childNodes;
		if(navigator.appName == "Netscape"){
			if(titleDiv[1].nodeName == 'DIV'){
				titleDiv[1].style.display="block";
				gTitleDiv = titleDiv[1];
				/*opacity(num);*/
				for(var j=0; j<popupId.length; j++){
					if(popupId[j].nodeName == "DIV"){
							if(t){
							clearTimeout(t);
							}
					}
				}
			}
		}else if(navigator.appName == "Microsoft Internet Explorer"){
				if(titleDiv[0].nodeName == 'DIV'){
				titleDiv[0].style.display="block";
				gTitleDiv = titleDiv[0];
				/*opacity(num);*/
				for(var j=0; j<popupId.length; j++){
					if(popupId[j].nodeName == "DIV"){
							if(t){
							clearTimeout(t);
							}
					}
				}
			}
		}
		
		var onfocus = document.getElementById('onFocus');
		if(onfocus != null){
		onfocus.focus();
		}
		controlAllSelect('0');
}

function closePopup(num, Id) {
		var popupDiv = document.getElementById(num);
		if(Id != 'noId'){
		var popupIdd = document.getElementById(Id);
		popupIdd.style.display="none";
		}
		popupDiv.style.display="none";
		var titleDiv = popupDiv.childNodes;
		if(navigator.appName == "Netscape"){
			if(titleDiv[1].nodeName == 'DIV'){
				titleDiv[1].style.display="none";
			}
			clearTimeout(t);
		}else if(navigator.appName == "Microsoft Internet Explorer"){
			if(titleDiv[0].nodeName == 'DIV'){
				titleDiv[0].style.display="none";
			}
			clearTimeout(t);
		}
		controlAllSelect('1');
}

function hiddenSelect(){
	document.all.select1.style.visibility="hidden";
	document.all.select2.style.visibility="hidden";
	document.all.select3.style.visibility="hidden";
}

function stopCount(num)
{
clearTimeout(num);
}

function controlAllSelect(num){
	var check = num;
	var selectList = document.getElementsByTagName('select');
	var listLength = selectList.length;
	for(var a = 0; a < listLength; a++){
		if(check == 0){
		selectList[a].style.display = 'none';
		}else if(check == 1){
			selectList[a].style.display = 'inline';
		}
	}
	
}

function controlAllParentSelect(num){
	var check = num;
	var selectList = parent.window.document.getElementsByTagName('select');
	var listLength = selectList.length;
	for(var a = 0; a < listLength; a++){
		if(check == 0){
		selectList[a].style.display = 'none';
		}else if(check == 1){
			selectList[a].style.display = 'inline';
		}
	}
	
}

function showSelect(node){
	if (node) node.style.display = 'inline';
}

function hideSelect(node){
	if (node) node.style.display = 'none';
}

function hideElementById(elementId){
	var targetElement = document.getElementById(elementId);
	if(targetElement)
		targetElement.style.display='none';
}

function displayElementById(elementId){
	var targetElement = document.getElementById(elementId);
	if(targetElement)
		targetElement.style.display='block';	
}

function preSelectOption(select, value){
	var selectElement = document.getElementById(select);
	for ( i=0; i<selectElement.length;i++){		
		if (selectElement[i].value == value){
			selectElement.selectedIndex = i;
			break;
		}
	}	
}

function limitImageSize(image,width,height,enforce) {
    var originalWidth = image.width;
    var originalHeight = image.height;
    if(image.complete == false) {
        var newImageObj = new Image();
		newImageObj.src = image.src;
		// fix for IE7 which would give image width and height of 0
		originalWidth = newImageObj.width;
		originalHeight = newImageObj.height;
    }
	if (width>=0) {
		if (enforce || originalWidth>width) image.width=width;
	}
	if (height>=0) {
		if (enforce || originalHeight>height) image.height=height;
	}
}

function decimal(number,decimal) {
	var ans = number * Math.pow(10,decimal+1);
	ans = Math.round(ans /Math.pow(10,decimal-1)) + "";
	while (ans.length < decimal+1) { ans = "0" + ans; }
	var len = ans.length;
	ans = ans.substring(0,len-decimal) + "." + ans.substring(len-decimal,len);
	return ans;
}

function redirect(url) {
    if(url != null)
    window.location = url;
}

function showLongContent(contentKey, width, height){
    showDialog('/corel/include/longContent.jsp?contentKey='+contentKey, width, height);
}

function highlight(element) {
    if(element) {
	    element.style.borderColor = '#FF0000';
	    element.style.backgroundColor = '#FF9999';
	    element.style.color = '#000000';
	    element.style.borderWidth = '1px';
    }
}

function deHighlight(element) {
    if(element) {
	    element.style.borderColor = '';
	    element.style.backgroundColor = '';
	    element.style.color = '';
	    element.style.borderWidth = '';
    }
}

String.prototype.stripHtml = function () {
	   return this.replace('&amp;','&').replace('&amp;','&');
};
