var allChecked=false;

// moved functiona from login pages to one place
function ChangeLinks(){
    var obj = eval("document.login_frm");
    var div_username = document.getElementById("div_username");

    if (obj){
      if(obj.type[0].checked) {
	    document.getElementById("div_cust_links").style.display = "block";
	    document.getElementById("div_aff_links").style.display = "none";
	    div_username.innerHTML = "Email";
      }
      else {
	    document.getElementById("div_cust_links").style.display = "none";
	    document.getElementById("div_aff_links").style.display = "block";
	    div_username.innerHTML = "Username";
      }
    }
 }
  
function changeFocus(_field){
      var obj = eval("document.login_frm."+_field);
      if (obj){
	if(_field == 'username' && obj.value == 'Your User Name') {
	    obj.value = '';
	}
	else if(_field == 'password' && obj.value == 'password') {
	    obj.value = '';
	}
      }
}
// moved isValidEmail to one place

// moved from the admin files
function CurrencyFormatted(amount)
{
    var i = parseFloat(amount);
    //if(isNaN(i)) { i = 0.00; }
    if(isNaN(i)) { return ""; }
    var minus = '';
    if(i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if(s.indexOf('.') < 0) { s += '.00'; }
    if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    return s;
}

function MoneyInput(e) {
	if (!e)
	e = window.event;
	var targ;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;

	if (e.keyCode) kcode = e.keyCode;
	else if (e.which) kcode = e.which;

	if ((kcode == 8) || (kcode == 39) || (kcode == 190)|| (kcode == 110) || (kcode == 37) || (kcode > 45 && kcode <= 57) || (kcode > 96 && kcode <= 105))
	{
		e.returnValue = true;
	}
	else
	{	
		e.returnValue = false;
	}
	return e.returnValue;
}
	
function outMoneyFormat(field)
{
	var value = jQuery("#"+field+"").val();
	jQuery("#"+field+"").val(CurrencyFormatted(value));
}

function DecimalInput(e) {
	if (!e)
	e = window.event;
	var targ;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;

	if (e.keyCode) kcode = e.keyCode;
	else if (e.which) kcode = e.which;

	if ((kcode == 8) || (kcode == 39) || (kcode == 190)|| (kcode == 110) || (kcode == 37) || (kcode > 45 && kcode <= 57) || (kcode > 96 && kcode <= 105))
	{
		e.returnValue = true;
	}
	else
	{	
		e.returnValue = false;
	}
	return e.returnValue;
}

function Check(_form, ischecked)
{
	var form = eval("document."+_form);
	
	chk = form.selall;
	ischecked = form.Check_ctr.checked;
	
	if(ischecked==true)
	{
		for (i = 0; i < chk.length; i++)
		chk[i].checked = true ;
	}
	else
	{
		for (i = 0; i < chk.length; i++)
			chk[i].checked = false;
	}
}

// moved from the admin files
	
function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (1) {
            curtop+=obj.offsetTop;
            if (!obj.offsetParent) {
                break;
            }
            obj=obj.offsetParent;
        }
    } else if (obj.y) {
        curtop+=obj.y;
    }
    return curtop;
}

function addImage(fileUrl) {
	findObj('previewImage').src = fileUrl;
	findObj('imageName').value = fileUrl;
}

function addRows(parent, quantity) {
	var qty		= document.getElementById(quantity).value;
	var rowCount	= document.getElementById('rowCount').value;
	var parentNode	= document.getElementById(parent);

	qty = (qty >= 1)  ? qty: 1;
	qty = (qty <= 20) ? qty: 20;

	for (i=0; i<qty; i++) {
		var newRow	= document.createElement('tr');
		newRow.setAttribute('id', 'orderRow_'+rowCount);
		parentNode.appendChild(newRow);

		var newRowValue = '<td valign="top">';
		newRowValue += '<a href="" onclick="return delRow(\'orderRow_'+rowCount+'\')"><img src="admin/images/del.gif" alt="" /></a>';

		newRowValue += '</td><td valign="top">';
		newRowValue += '<input type="text" name="shipped['+rowCount+']" id="shipped_['+rowCount+']" style="text-align:center;" size="3" value="" />';

		newRowValue += '</td><td valign="top">';
		newRowValue += '<input type="hidden" name="id['+rowCount+']" value="" />';
		newRowValue += '<input type="text" name="prodName['+rowCount+']" class="textbox" value="" size="40"/>';
		newRowValue += '</td><td valign="top">';
		newRowValue += '<input name="productCode['+rowCount+']" type="text" class="textbox" value="" size="15" />';
		newRowValue += '</td><td valign="top">';
		newRowValue += '<textarea name="product_options['+rowCount+']" cols="30" rows="2" class="textbox"></textarea>';

		newRowValue += '</td><td align="center" valign="top"><br/>';

		newRowValue += '</td><td align="center" valign="top">';
		newRowValue += '<input name="quantity['+rowCount+']" type="text" class="textbox" style="text-align:center;" value="" size="3" />';
		newRowValue += '</td><td valign="top" align="center">';
		newRowValue += '<input name="price['+rowCount+']" type="text" class="textbox" style="text-align:right;" value="" size="7" />';

		newRowValue += '</td><td align="center" valign="top"><br/>';
		newRowValue += '</td><td align="center" valign="top"><br/>';

		newRowValue += '</td>';

		jQuery("#orderRow_"+rowCount).html(newRowValue);
		
		//newRow.innerHTML = newRowValue;
		rowCount++;
	}
	document.getElementById('rowCount').value = rowCount;
	return false;
}


function addProductRow(parent, quantity) {
	var qty		= document.getElementById(quantity).value;
	var rowCount	= document.getElementById('rowCount').value;
	var parentNode	= document.getElementById(parent);

	var productName = "";
	var productCode = "";
	var productPrice = "";
	var productCostPrice = "";
	var productOptions = "";
	var productString = "";
	var productArray = new Array();
    
	var mutliProducts = jQuery("#addProductIdSelect");    
			
	var productString = mutliProducts.val().toString();			
	productArray = productString.split('^');

	if(productArray[0] && productArray[0]!="")
		productName = productArray[0];

	if(productArray[1] && productArray[1]!="")
		productCode = productArray[1];

	if(productArray[2] && productArray[2]!="")
		productPrice = productArray[2];

	if(productArray[3] && productArray[3]!="")
		productCostPrice = productArray[3];

	if(productArray[4] && productArray[4]!="")
	{
		productOptions = productArray[4];
		productOptions = productOptions.replace("<br/>", "\n");
	}

	if(productArray)
	{
			var newRow	= document.createElement('tr');					
			newRow.setAttribute('id', 'orderRow_'+rowCount);
			parentNode.appendChild(newRow);

			var newRowValue = '<td valign="top">';
			newRowValue += '<a href="" onclick="return delRow(\'orderRow_'+rowCount+'\')"><img src="admin/images/del.gif" alt="" /></a>';
			newRowValue += '</td><td valign="top">';
			newRowValue += '<input type="text" name="shipped['+rowCount+']" id="shipped_['+rowCount+']" style="text-align:center;" size="3" value="0" />';
			newRowValue += '</td><td valign="top">';
			newRowValue += '<input type="hidden" name="id['+rowCount+']" value="" /><input type="hidden" name="default_price['+rowCount+']" id="default_price_'+rowCount+'" value="'+productPrice+'" />';
			newRowValue += '<input type="text" name="prodName['+rowCount+']" class="textbox" value="' + productName + '" size="40"/>';
			newRowValue += '</td><td valign="top">';
			newRowValue += '<input name="productCode['+rowCount+']" type="text" class="textbox" value="' + productCode + '" size="15" />';
			newRowValue += '</td><td valign="top">';
			newRowValue += '<textarea name="product_options['+rowCount+']" cols="30" rows="2" class="textbox">' + productOptions +'</textarea>';
			newRowValue += '</td><td align="center" valign="top"><br/>';
			newRowValue += '</td><td align="center" valign="top">';
			newRowValue += '<input id="quantity_'+rowCount+'" name="quantity['+rowCount+']" type="text" class="textbox" onblur="CalculateProductPrice('+rowCount+');" style="text-align:center;" value="1" size="3" />';
			newRowValue += '</td><td valign="top" align="center">';
			newRowValue += '<input name="unit_price['+rowCount+']" id="unit_price_'+rowCount+'" type="text" class="textbox" style="text-align:right;" value="' + productPrice + '" size="7" /></td>';
			
			if(productCostPrice!="hidden")
			{
				newRowValue += '<td align="center" valign="top">$' + productCostPrice + '</td>';
			}
			newRowValue += '<td align="center" valign="top">';
			newRowValue += '<input name="price['+rowCount+']" id="price_'+rowCount+'" type="text" class="textbox" style="text-align:right;" value="' + productPrice + '" size="7" />';
			newRowValue += '</td>';					
									
			jQuery("#orderRow_"+rowCount).html(newRowValue);										
			rowCount++;
	}		
	document.getElementById('rowCount').value = rowCount;
	return false;
}

function checkAll(fieldName,val) {
	var checks = findObj(fieldName)
	for (i=0; i<checks.length; i++) {
		if(val == 'true') {
			checks[i].checked = true;
		} else {
			checks[i].checked = false;
		}
	}
}

function checkUncheck(parent, className) {
	var elements = $(parent).getElementsByClassName(className);
	for (i=0;i<elements.length;i++) {
		var ele = elements[i];
		(allChecked==false) ? ele.checked='checked' : ele.checked='';
	}
	(allChecked!=true) ? allChecked=true : allChecked=false;
	return false;
}

function compareInputbox(idNo) {
	if (findObj('custom_'+idNo) != findObj('default_'+idNo)) {
		// show revert buttons
		findObj('revertLink_'+idNo).style.display = '';
		findObj('revAllLink').style.display='';
		var tds = findObj('tr_'+idNo).getElementsByTagName('td');
		for (var i = 0; i <= 2; i++) {
			tds[i].className = 'tdModified';
		}
	}
}

function decision(message, url) {
	if (confirm(message)) {
		location.href = url;
	} else {
		return false;
	}
}

function delRow(element) {
	var node	= document.getElementById(element);
	var parent	= node.parentNode;
	parent.removeChild(node);
	return false;
}

function disableSubmit(obj,msg) {
	obj.value=msg;
	obj.disabled=true;
	obj.disabled=true;
	obj.className='submitDisabled';
}

function displayStatusMsg(msgStr) { //v1.0
	status=msgStr;
	document.returnValue = true;
}

function editVal(id,val) {
	findObj(id).value = val;
}

function findObj(n, d) {
	var p,i,x;
	if (!d) d=document;
	if ((p=n.indexOf("?"))>0&&parent.frames.length){
		d=parent.frames[n.substring(p+1)].document;
		n=n.substring(0,p);
	}
	if (!(x=d[n])&&d.all) x=d.all[n];
	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for (i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
	if (!x && d.getElementById) x=d.getElementById(n);
	return x;
}

function flashBasket(flashes) {
	setTimeout("flash("+flashes+")", 500);
}

function flash(flashes) {
	var targetBtn = findObj('flashBasket');
	if (flashes<=0) return;
	if ((flashes%2) == 0) {
		targetBtn.className="flashBasket";
	} else {
 		targetBtn.className="txtviewCart";
	}
	setTimeout("flash("+(flashes-1)+")", 300);
	return;
}

function getImage(imageName) {
	var img	= findObj('img');
	img.src	= img.src.replace(/language\/[a-z\-_]{2,5}\/flag.gif/gi, imageName);
}

function setMainImage(imageName) {
	findObj('img').src = imageName;
}

function goToURL() { //v3.0
  var i, args=goToURL.arguments; document.returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function jumpMenu(target, object, restore) {
	eval(target+".location='"+object.options[object.selectedIndex].value+"'");
	if (restore) object.selectedIndex=0;
}

function menuBlinds() {

}

function openPopUp(url, windowName, w, h, scrollbar) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;	
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scrollbar+',resizable=1';
	win = window.open(url, windowName, winprops);
	if (parseInt(navigator.appVersion) >= 4) {
		win.window.focus();
	}
}

function resizeOuterTo(w,h) {
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			top.outerWidth=w;
			top.outerHeight=h;
		} else {
			top.resizeTo(w,h);
		}
	}
}

function revert(idNo,className) {
	var tds = findObj('tr_'+idNo).getElementsByTagName('td');
	for (var i = 0; i <= 2; i++) {
		tds[i].className = className;
	}
	findObj('custom_'+idNo).value = findObj('default_'+idNo).value;
	findObj('revertLink_'+idNo).style.display = 'none';
}

function revertAll(maxId) {
	for (var idNo = 1; idNo < maxId; idNo++) {
		// find array of TD's
		var tds = findObj('tr_'+idNo).getElementsByTagName('td');
		var binary = idNo.toString(2);
		for (var j = 0; j <= 2; j++) {
			// restore class
			if (binary.charAt(binary.length - 1) == "1") {
				tds[j].className = 'tdOdd';
			} else {
				tds[j].className = 'tdEven';
			}
		}
		// set default value back
		findObj('custom_'+idNo).value = findObj('default_'+idNo).value;
		// hide revert button
		findObj('revertLink_'+idNo).style.display = 'none';
	}
	// hide revert all button
	findObj('revAllLink').style.display='none';
}

function setTextOfLayer(objName,x,newText) {
	if ((obj=findObj(objName))!=null) with (obj)
		if (document.layers) {document.write(unescape(newText)); document.close();}
		else innerHTML = unescape(newText);
}

function submitDoc(formName) {
	var obj=findObj(formName);
	if (obj!=null) {
		obj.submit();
	} else {
		alert('The form you are attempting to submit called \'' + formName + '\' couldn\'t be found. Please make sure the submitDoc function has the correct id and name.');
	}
}


function submitDoc2(formName) {
	var obj=findObj(formName);
	if (obj!=null) {
		obj.frmbutton.value="1";
		obj.submit();
	} else {
		alert('The form you are attempting to submit called \'' + formName + '\' couldn\'t be found. Please make sure the submitDoc function has the correct id and name.');
	}
}

// autoship
function submitDocAuto(formName) {
	var obj=findObj(formName);
	if (obj!=null) {
		obj.asr.value="1";
                obj.action += "&asr=1";
		obj.submit();
	} else {
		alert('The form you are attempting to submit called \'' + formName + '\' couldn\'t be found. Please make sure the submitDocAuto function has the correct id and name.');
	}
}
function reloadPage(init) {  //reloads the window if Nav4 resized
	if (init==true) with (navigator) {
		if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    		document.pgW=innerWidth; document.pgH=innerHeight;
			onresize=reloadPage;
		}
	} else if (innerWidth!=document.pgW || innerHeight!=document.pgH) location.reload();
}
reloadPage(true);

function showHideLayers() { //v6.0
  var i,p,v,obj,args=showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function stars(rating, pathImg) {
	/* Positive Stars */
	for (var i = 0; i <= rating; i++) {
		if (i>0) findObj("star"+i).src = pathImg+'1.gif';
	}
	/* Negative Stars */
	for (var i = rating+1; i <= 5; i++) {
		findObj("star"+i).src = pathImg+'0.gif';
	}
	findObj('rating_val').value=rating;
}

function toggleReg() {
	var password = findObj('password');
	var passwordConf = findObj('passwordConf');
	if (password.disabled == false && passwordConf.disabled==false) {
		password.disabled=true;
		passwordConf.disabled=true;
		password.className="textboxDisabled";
		passwordConf.className="textboxDisabled";
	} else {
		password.disabled=false;
		passwordConf.disabled=false;
		password.className="textbox";
		passwordConf.className="textbox";
	}
	password.value="";
	passwordConf.value="";
}

function toggleProdStatus(i,messageRemove, messageNotRemoved,removeImgSrc,noRemoveImgSrc){
	if(findObj('delId['+i+']').value==1) {
		var result = false;
		var className = 'textbox';
		findObj('del['+i+']').src = removeImgSrc;
		findObj('delId['+i+']').value = 0;
		// change class
		findObj('prodName['+i+']').className = "textbox";
		findObj('productCode['+i+']').className = "textbox";
		findObj('product_options['+i+']').className = "textbox";
		findObj('quantity['+i+']').className = "textbox";
		findObj('price['+i+']').className = "textbox";
		alert(messageNotRemoved);
	} else {
		var result = true;
		var className = 'textboxDisabled';
		findObj('del['+i+']').src = noRemoveImgSrc;
		findObj('delId['+i+']').value = 1;
		// change class
		findObj('prodName['+i+']').className = "textboxDisabled";
		findObj('productCode['+i+']').className = "textboxDisabled";
		findObj('product_options['+i+']').className = "textboxDisabled";
		findObj('quantity['+i+']').className = "textboxDisabled";
		findObj('price['+i+']').className = "textboxDisabled";
		alert(messageRemove);
	}

	/*findObj('name['+i+']').disabled = result;
	findObj('name['+i+']').className = className;

	findObj('productCode['+i+']').disabled = result;
	findObj('productCode['+i+']').className = className;

	findObj('product_options['+i+']').disabled = result;
	findObj('product_options['+i+']').className = className;

	findObj('quantity['+i+']').disabled = result;
	findObj('quantity['+i+']').className = className;

	findObj('price['+i+']').disabled = result;
	findObj('price['+i+']').className = className;*/
}


function toggleProductStatus() {
	return false;
}

function goUrl(element) {
	var url = document.getElementById(element).options[document.getElementById(element).selectedIndex].value;
	window.location = url;
}



/* Start Cross-Browser DHTML Menu */
function showSubMenu() {
	var objThis = this;
	for (var i = 0; i  < objThis.childNodes.length; i++) {
		if (objThis.childNodes.item(i).nodeName == "UL")	{
			objThis.childNodes.item(i).style.display = "block";
		}
	}
}

function hideSubMenu() {
	var objThis = this;
	for (var i = 0; i  < objThis.childNodes.length; i++) {
		if (objThis.childNodes.item(i).nodeName == "UL") {
			objThis.childNodes.item(i).style.display = "none";
			return;
		}
	}
}

function initialiseMenu() {
	var objLICollection = document.body.getElementsByTagName("LI");
	for (var i = 0; i < objLICollection.length; i++) {
		var objLI = objLICollection[i];
		for (var j = 0; j  < objLI.childNodes.length; j++) {
			if(objLI.childNodes.item(j).nodeName == "UL") {
				objLI.onmouseover=showSubMenu;
				objLI.onmouseout=hideSubMenu;
				for (var j = 0; j  < objLI.childNodes.length; j++) {
					if(objLI.childNodes.item(j).nodeName == "A") {
						objLI.childNodes.item(j).className = "hassubmenu";
					}
				}
			}
		}
	}
}
/* End Cross-Browser DHTML Menu */

/* New Options code */
function optionEdit(assign_id, optionData) {
	var data		= optionData.split('|');
	var elements	= $('opt_mid').getElementsByTagName('option');
	for (i=0; i<elements.length; i++) {
		elements[i].removeAttribute('selected');

		if (data[1] == '0') {
			if (elements[i].value == data[0] && elements[i].getAttribute('class') == 'top') {
				elements[i].setAttribute('selected', 'selected');
				elements[i].selected = true;
			}
		} else {
			if (elements[i].value == data[1] && elements[i].getAttribute('class') == 'sub') {
				elements[i].setAttribute('selected', 'selected');
				elements[i].selected = true;
			}
		}
	}
	$('opt_price').value = data[2];
	$('opt_assign_id').value = assign_id;

	optionRemove(assign_id, true);
}

function optionRemoveTemp(element) {
	var object = $(element).parentNode;
	$('options_added').removeChild(object);
}

function optionRemove(id, preserve) {
	$('option_'+id).remove();
	if (!preserve) {
		new Insertion.Bottom($('options_added'), '<input type="hidden" name="option_remove[]" value="'+id+'"/>');
	}
}

function optionAdd() {

	var assign_id = $('opt_assign_id').value;

	var opt_top_title = $('opt_mid').options[$('opt_mid').selectedIndex].parentNode.getAttribute('label');
	var opt_mid_title = $('opt_mid').options[$('opt_mid').selectedIndex].innerHTML;

	var opt_mid_value	= $('opt_mid').options[$('opt_mid').selectedIndex].value;
	var opt_top_value	= $('opt_mid').options[$('opt_mid').selectedIndex].parentNode.id;

	var opt_price	= $('opt_price').value;
	opt_price		= (!opt_price) ? 0.00 :  opt_price;

	if (opt_top_value != '' && opt_mid_value != '') {
		if (assign_id != '0') {
			var method	= 'option_edit['+assign_id+']';
			$('opt_assign_id').value = '0';
		} else {
			var method	= 'option_add[]';
		}
		if (opt_top_title == null) opt_top_title = 'Custom';
		var hidden = '<input type="hidden" name="'+method+'" value="'+opt_top_value+'|'+opt_mid_value+'|'+opt_price+'"/>';
		new Insertion.Bottom('options_added', '<div style="clear: right;">'+hidden+'<span style="float: right;"><a href="#" onclick="optionRemoveTemp(this.parentNode); return false;"><img src="images/icons/delete.png" alt="delete" /></a></span><strong>'+opt_top_title+'</strong>: '+opt_mid_title+' ('+opt_price+')</div>');
		$('opt_price').value = '0.00';
	}
}

function gradient(id, level)
{
	var box = document.getElementById(id);
	box.style.opacity = level;
	box.style.MozOpacity = level;
	box.style.KhtmlOpacity = level;
	//box.style.filter = "alpha(opacity=" + level+ ")";
	box.style.display="block";
	return;
}

function fadein(id)
{
	var level = 0;
	while(level <= 1)
	{
		setTimeout( "gradient('" + id + "'," + level + ")", (level* 1000) + 10);
		level += 0.01;
	}
}

// Open the lightbox

function openbox(formtitle, fadin)
{
  var box = document.getElementById('box');

  document.getElementById('filter').style.display='block';

  var btitle = document.getElementById('boxtitle');
  btitle.innerHTML = formtitle;

  if(fadin)
  {
	 gradient("box", 0);
	 fadein("box");
	 box.style.display='block';
  }
  else
  {
    box.style.display='block';
  }

}

// Close the lightbox

function closebox()
{
   document.getElementById('box').style.display='none';
   document.getElementById('filter').style.display='none';
}

function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')	{
    	window.onload = func;
	} else {
		window.onload = function()
		{
			oldonload();
			func();
		};
	}
}




