// -- LIBRARY FUNCTIONS



// CHECK BOX AND COMBOBOX HANDLER - TO DO SELECT ALL / DESELECT ALL COMMANDS
function CheckBoxes_SetValue(GroupName, Value) {
	for (i = 0; i < document.forms.length; i++)
		for (j = 0; j < document.forms[i].elements.length; j++) {
			field = document.forms[i].elements[j];
			if (field.type != "checkbox") continue;
			if (!field.name) continue;
			if (!field.disabled && field.name.indexOf(GroupName) >= 0) field.checked = Value;
			}
}
function ComboBoxes_SetValue(GroupName, Value) {
	for (i = 0; i < document.forms.length; i++)
		for (j = 0; j < document.forms[i].elements.length; j++) {
			field = document.forms[i].elements[j];
			if (field.type != "select-one") continue; // skip if element is not a combo box
			if (!field.name) continue; // skip if name is not provided to the field
			if (!field.disabled && field.name.indexOf(GroupName) >= 0) field.value = Value;
			}
}




// TO SHOW AND HIDE LAYERS  ... ALSO SUPPORTS Z-INDEXING
var zIndex = 200;
function getElementPosition(offsetTrail) {
	var offsetLeft = 0;
	var offsetTop = 0;
	while (offsetTrail) {
		offsetLeft += offsetTrail.offsetLeft;
		offsetTop += offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
		}
	if (navigator.userAgent.indexOf("Mac") != -1) {
		offsetLeft += document.body.leftMargin;
		offsetTop += document.body.topMargin;
		}
	return {left:offsetLeft, top:offsetTop};
}
function ShowLayer(layerID, CallerObj, Event, OffsetX, OffsetY) {
	if (!document.getElementById(layerID)) return;

	if (document.all) {
		x = window.event.clientX + document.body.scrollLeft;
		y = window.event.clientY + document.body.scrollTop;
	} else {
		x = Event.pageX;
		y = Event.pageY;
		}

	document.getElementById(layerID).style.left = x + OffsetX;
	document.getElementById(layerID).style.top = y + OffsetY;
	document.getElementById(layerID).style.visibility = 'visible';
	document.getElementById(layerID).style.display = 'block';
	document.getElementById(layerID).style.zIndex = zIndex;
	zIndex++;
}

function HideLayer(layerID) {
	document.getElementById(layerID).style.visibility = 'hidden';
}






// AJAX IMPLEMENTATION
function NewXmlHttp() {
	var xhttp;
	if (document.all) {
		try { xhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) {
			try { xhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch (E) { return null; }
			}
	} else xhttp = new XMLHttpRequest();
	return xhttp;
}

function SendHttpRequest(URI, Variables, xhttp, Handler) {
	try {
		//if (typeof netscape != "undefined") netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalBrowserRead");
		xhttp.onreadystatechange = Handler;
		xhttp.open("POST", URI, true);
		xhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xhttp.send(Variables);
		return true;
	} catch (e) {
		errStr = "Error occurred while processing:\n";
		if (document.all) errStr += e.message; else errStr += e;
		alert(errStr);
		return false;
		}
}
















function AddToFavorites_MakeLink() {
	Browser = "";
	if (window.sidebar) Browser = "FIREFOX";
	if (Browser == "" && window.external) Browser = "IE";
	if (Browser == "" && window.opera && window.print) Browser = "OPERA";

	link = "<a href='javascript:AddToFavorites_Add();' class='LeftBar'>";
	link += "<img src='req/btn_favorites.jpg' style='width:180px;height:50px;' />";
	link += "</a>";
	document.write(link);
}

function AddToFavorites_Add() {
	title = "Competitive Commercial Carpet - Homepage";
	url = "http://www.competitivecommercialcarpet.com";
	switch (Browser) {
		case "FIREFOX" : window.sidebar.addPanel(title, url,""); break;
		case "IE" : window.external.AddFavorite( url, title); break;
		case "OPERA" : return true; break;
		}
}






// -- SECTION FOR SEE SHIPPING TOTAL
var FreightCalculator_CategoryID = 0;
function ShowTotalWithShipping(obj, e, Price, CID) {
	HideLayer("divShowTotal");

	//if (isNaN(document.getElementById("txtShippingQuantity").value)) document.getElementById("txtShippingQuantity").value = 0;
	//if (document.getElementById("txtShippingQuantity").value == "") document.getElementById("txtShippingQuantity").value = 0;
	document.getElementById("txtShippingQuantity").value = 0;
		document.getElementById("txtZipCode").value = "";
	
	
//	document.getElementById("spnShippingProductPrice").value = 0;
	document.getElementById("spnShippingSubTotal1").innerHTML = 0;
	document.getElementById("spnShippingPrice").innerHTML = 0;
	document.getElementById("spnShippingTotal").innerHTML = 0;
		document.getElementById("spnShippingProductPrice").innerHTML = Price.toFixed(2);
	document.getElementById("hdnCID").value=CID;	
	FreightCalculator_CategoryID = CID;
	ShowLayer("divShowTotal", obj, e, -400, -20);
//FreightCalculator_CategoryID=60;
	if(FreightCalculator_CategoryID==60)
	{
		document.getElementById("divMesure").style.display="none";
		//document.getElementById("divMesureLF").style.display="block";
		document.getElementById("LF").style.display="block";
		document.getElementById("SpnLFYards").	innerHTML="Price Per Foot:";	
		document.getElementById("spnCarpetTotal").	innerHTML="Carpet Base Total:";	
		document.getElementById("spnEnterAmount").	innerHTML="Enter Amount of Base:";	
	}
	//CalculateShippingTotal(FreightCalculator_CategoryID);
	return false;
}
/*
function CalculateShippingTotal(CID) {
	Qty = Number(document.getElementById("txtShippingQuantity").value.replace(",", ""));
	if (frmShippingDetails.rdoShippingQuantityUnits[0].checked) UnitFactor = 1; else UnitFactor = 1/9;
	p = Math.ceil(Qty * UnitFactor);
	Freight = 0;

	Price = Number(document.getElementById("spnShippingProductPrice").innerHTML);
	SubTotal1 = Qty * Price * UnitFactor;
	document.getElementById("spnShippingSubTotal1").innerHTML = isNaN(SubTotal1) ? " ERROR" : SubTotal1.toFixed(2);
	if (CID == 9 || CID == 82 || CID == 82 || CID == 83 || CID == 101 || CID == 234 ) {
		Shipping = 0;
		if (p >= 1 && p <= 200) Shipping = 398.09;
		if (p >= 201 && p <= 299) Shipping = 297.89;
		//if (p >= 300) Shipping = Qty * UnitFactor * 1.174;
		
	} else {
			if(CID==60) {
				Shipping=0;
				if(p<=250) Shipping=72.25;
				if(p>=251) Shipping=(((p-250)*0.09)+72.25);
			} else {
				Shipping = 0;
				if (p >= 1 && p < 201) Shipping =  392.22;
				if (p >= 201 && p <= 299) Shipping = 289.17;
				if (p >= 300) Shipping = Qty * UnitFactor * 0.779;
				}
		}

	document.getElementById("spnShippingPrice").innerHTML = isNaN(Shipping) ? " ERROR" : Shipping.toFixed(2);
	Total = SubTotal1 + Shipping;
	document.getElementById("spnShippingTotal").innerHTML = isNaN(Total) ? " ERROR" : Total.toFixed(2);
	return false;
}
*/ 













function ValidateSendSampleRequest() {
	var errMsg="";
	var name = document.frm.contactname.value;

	if (name == "")
		errMsg += "Missing: Contact Name\n";
	else {
		arrName = name.split(" ");
		if (name.length < 7 || arrName.length < 2) errMsg += "Invalid name. Full name required.\n";
		}
	if (document.frm.address.value=="") errMsg += "Missing: Address\n";
	if (document.frm.city.value=="") errMsg += "Missing: City\n";
	if(document.getElementById('country').value=="USA")
	{
		if (document.frm.state.value=="") errMsg += "Missing: State\n";
	}else{
		if (document.frm.txtState.value=="") errMsg += "Missing: Province/State\n";
	}
	if (document.frm.zip.value=="") errMsg += "Missing: Zip\n";
	if (document.frm.email.value=="") errMsg += "Missing: Email\n";
	if (document.frm.phone1.value=="") errMsg += "Missing: First Phone Block\n";
	if (document.frm.phone2.value=="") errMsg += "Missing: Second Phone Block\n";
	if (document.frm.phone.value=="") errMsg += "Missing: Third Phone Block\n";
	
	var regexp = /^\d{4}$/;
	var regexp1 = /^\d{3}$/;
	var regexp2 = /^\d{3}$/;
	
	if(document.frm.phone1.value !=''){
		if(regexp1.test(document.frm.phone1.value)==false)
		{
			errMsg += "Invalid: Fist Phone Number Block,Must have 3 digit length\n";
		}
	}
		if(document.frm.phone2.value !=''){
		if(regexp2.test(document.frm.phone2.value)==false)
		{
			errMsg += "Invalid: Second Phone Number Block,Must have 3 digit length\n";
		}
	}
	
	if(document.frm.phone.value !=''){
		if(regexp.test(document.frm.phone.value)==false)
		{
			errMsg += "Invalid: Third Phone Number Block,Must have 4 digit length\n";
		}
	}
	
	if (isNaN(document.frm.Quantity.value)) errMsg += "Error: Amount of Carpet must be numeric value\n";
	if(document.frm.Quantity.value!="")
	{
		if(!document.frm.yf[0].checked && ! document.frm.yf[1].checked) errMsg+= "Missing: Please choose a unit of measurement (sq. yards or sq. feet).\n";
	}
		if(document.frm.txtOdering.value!=1)	{
			if (document.frm.Quantity.value=="") errMsg += "Missing: Amount of Carpet Required in Your Project\n";
		}
	if(document.frm.txtOdering.value==1)
	{
	//	if(!document.frm.yfO[0].checked && ! document.frm.yfO[1].checked ) errMsg+= "Missing: Please choose a unit of measurement (sq. yards or sq. feet) for Amount of Carpet Base in Project.\n";

	if(document.frm.CoveBase.value==""  || document.frm.CoveBase.value<1 ) errMsg+= "Missing: Amount of Cove Base in Your Project.\n";

	}
	if (errMsg!="") {
		alert(errMsg);
		return false;
	} else {
		document.frm.method="post";
		document.frm.submit();
		}
}






function ShowSendToFriend(obj, e, productID) {
	ShowLayer("divReferFriend", obj, e, -400, -20);
	document.frmReferFriend.ProductID.value = productID;
}

function ValidateSendToFriend() {
	var errMsg = "";
	if (document.frmReferFriend.txtNameS.value == "") errMsg += "Missing your name\n";
	/*
	if (document.frmReferFriend.txtEmailS.value == "") {
		errMsg += "Missing your email address\n";
	} else {
		if (!ValidateEmailAddress(document.frmReferFriend.txtEmailS.value)) errMsg += "Your email address is invalid\n"
		}
	*/
	if (document.frmReferFriend.txtNameR.value == "") errMsg += "Missing recipient's name\n";
	if (document.frmReferFriend.txtEmailR.value == "") {
		errMsg += "Missing your email address\n";
	} else {
		if (!ValidateEmailAddress(document.frmReferFriend.txtEmailR.value)) errMsg += "Recipient's email address is invalid\n"
		}
	if (errMsg != "") {
		alert(errMsg);
		return false;
	} else return true;
}


function ValidateEmailAddress(str) {
	var RegEx  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return RegEx.test(str);
}




function ShowEditSubCategory(obj, e, productID,ColorName,SKU,ColorCode,AdminColorName,StockQty,ColorFamily) {
	ShowLayer("divEditSubCategory", obj, e, -380, -20);
	document.frmEditSubCategory.txtProductID.value=productID;
	document.frmEditSubCategory.txtOldSku.value=SKU;
	document.frmEditSubCategory.txtColorName.value = ColorName;
	document.frmEditSubCategory.txtColorCode.value = ColorCode;
	document.frmEditSubCategory.txtAdminColorName.value = AdminColorName;
	document.frmEditSubCategory.txtStockQty.value = StockQty;
	document.frmEditSubCategory.txtColorFamily.value = ColorFamily;
}

function ShowEditMainCategory(obj, e, productGroupId,ProductName,Description,Price,Cost,Width,ColorFamily,AdminStyle,IconId,productId,catId,MillName,UpdateDate) {

	ShowLayer("divEditMainCategory", obj, e, -380, -20);
	document.frmEditMainCategory.txtProductGroupId.value=productGroupId;
	document.frmEditMainCategory.txtProductName.value=ProductName;
	document.frmEditMainCategory.txtDescription.value=Description;
	document.frmEditMainCategory.txtPrice.value=Price;
	document.frmEditMainCategory.txtCost.value=Cost;
	document.frmEditMainCategory.txtWidth.value=Width;
	document.frmEditMainCategory.txtStyle.value=ColorFamily;
	document.frmEditMainCategory.txtAdminStyleName.value=AdminStyle;
	document.frmEditMainCategory.cboIcon.selectedIndex=IconId;
	document.frmEditMainCategory.txtOldProductName.value=ProductName;
	document.frmEditMainCategory.txtProductID.value=productId;
	document.frmEditMainCategory.cboCategory.value=catId;
	document.frmEditMainCategory.cboMillName.value=MillName;

	document.frmEditMainCategory.txtUpdateDate.value=UpdateDate;
}



function AlterOpacity(obj, alphaPercent) {
	if (document.all) {
		if (obj.filters && obj.filters.alpha) obj.filters.alpha.opacity = alphaPercent;
	} else {
		obj.style.opacity = alphaPercent / 100;
	}
}

function AlterOpacityOfAllTableCells(tableID, alphaPercent) {
	var tbl = document.getElementById(tableID);
	if (!tbl) return;

	for (i = 0; i < tbl.rows.length; i++)
		for (j = 0; j < tbl.rows[i].cells.length; j++) {
			AlterOpacity(tbl.rows[i].cells[j], alphaPercent);
			if (document.getElementById(tableID + "_imgClickToSee_" + (i + 1) + "_" + (j + 1))) 
				document.getElementById(tableID + "_imgClickToSee_" + (i + 1) + "_" + (j + 1)).style.display = "none";
			}
}
function ShowSendToRequest(obj, e) {
	ShowLayer("divRequestCall", obj, e, -200, 5);

}

function showSampleRequestPopuup(obj,e,StyleName,ColorName) {
	var x; 
	var d=navigator.appName;
	var browser=d.split(" ",1);
if (e.pageX!=null) x = e.pageX; else x = e.clientX;


	 if(x>900)
		{
			x=-460;	
		}
		else if(x>840)
		{
			x=-400;	
		}	
		else if(x>780)
		{
			x=-300;	
		}
		else if(x>670)
		{
				x=-280;	
		}
		else if(x>570)
		{
				x=-120;	
		}
		else
		{
			x=-20;
		}
	
		ShowLayer("divRequestSamples", obj, e, x, -10);	
		document.getElementById("styleName").innerHTML = StyleName;
		document.getElementById("spnColorName").innerHTML = ColorName;
}
function SelectedCountry(value)
{
	if(value!='USA')
	{
		document.getElementById('txtState').value='';
		document.getElementById('state').value="";
		document.getElementById('state').disabled='disabled';
		document.getElementById('spnStateLabel').style.display="inline";
		document.getElementById('spnStateText').style.display="inline";

	}else{
		document.getElementById('txtState').value='';
		document.getElementById('state').disabled=false;
		document.getElementById('spnStateLabel').style.display="none";
		document.getElementById('spnStateText').style.display="none";
		}
}

function EmailTrackerPopUp(obj,e)
{
		ShowLayer("divEmailTracker", obj, e, 20, -10);	
}



// COLOR FAMILY EDITOR
var ColorFamilies = new Array();

function ColorFamily_Get(productId) {
	for (i = 0; i < ColorFamilies.length; i++) {
		if (ColorFamilies[i].ProductID == productId) return ColorFamilies[i];
	}
	ColorFamilies.push({ ProductID: productId, Colors: new Array() });
	return ColorFamilies[ColorFamilies.length - 1];
}
function ColorFamily_AddColor(productId, color) {
	var cf = ColorFamily_Get(productId);
	cf.Colors.push(color);
}
function ColorFamily_CheckColor(productId, color) {
	var cf = ColorFamily_Get(productId);
	for (i = 0; i< cf.Colors.length; i++) {
		if (cf.Colors[i] == color) return true;
	}
	return false;
}
function CreateColorFamilyForms() {
	var html;
	for (i = 0; i < ColorFamilies.length; i++) {
		html = "";
		html += "<table style='width:180px;' cellpadding='0' cellspacing='5'>";
		html += GetColorFamilyFormRow(ColorFamilies[i].ProductID, "ivory", new Array("#FFFFF0"));
		html += GetColorFamilyFormRow(ColorFamilies[i].ProductID, "beige", new Array("#F5F5DC"));
		html += GetColorFamilyFormRow(ColorFamilies[i].ProductID, "wheat", new Array("#F5DEB3"));
		html += GetColorFamilyFormRow(ColorFamilies[i].ProductID, "tan", new Array("#D2B48C"));
		html += GetColorFamilyFormRow(ColorFamilies[i].ProductID, "khaki", new Array("#C3B091"));
		html += GetColorFamilyFormRow(ColorFamilies[i].ProductID, "grey", new Array("#EEEEEE", "#C0C0C0", "#808080", "#464646"), "silver");
		html += GetColorFamilyFormRow(ColorFamilies[i].ProductID, "blue", new Array("#000080", "#084C9E", "#0000CD", "#007FFF", "#00E5EE"));
		html += GetColorFamilyFormRow(ColorFamilies[i].ProductID, "green", new Array("#228B22", "#7FFF00", "#BFFF00", "green", "#00CD66", "#90EE90", "#CAFF70"));
		html += GetColorFamilyFormRow(ColorFamilies[i].ProductID, "red", new Array("#FF0000", "#FF6347", "#CD4F39"));
		html += GetColorFamilyFormRow(ColorFamilies[i].ProductID, "cyan", new Array("#00FFFF", "aqua", "#7FFFD4"));
		html += GetColorFamilyFormRow(ColorFamilies[i].ProductID, "teal", new Array("#008080"));
		html += GetColorFamilyFormRow(ColorFamilies[i].ProductID, "gold", new Array("#FFD700", "#DAA520", "#CDAD00", "#EEC900", "#8B7500"));
		html += GetColorFamilyFormRow(ColorFamilies[i].ProductID, "pink", new Array("#FC0FC0", "#FF77FF", "pink", "#EEA9B8", "#FF1493"));
		html += GetColorFamilyFormRow(ColorFamilies[i].ProductID, "purple", new Array("#4B0082", "#843179", "#0000CD", "#007FFF", "#551A8B", "#A020F0"));
		html += GetColorFamilyFormRow(ColorFamilies[i].ProductID, "yellow", new Array("#FFFF00", "#EEEE00", "#CDCD00", "#8B8B00", "#FFFFE0"));
		html += GetColorFamilyFormRow(ColorFamilies[i].ProductID, "orange", new Array("#FFA500", "#EE9A00", "#CD8500", "#FF4500"));
		html += GetColorFamilyFormRow(ColorFamilies[i].ProductID, "brown", new Array("#A52A2A", "#8B2323"));
		html += GetColorFamilyFormRow(ColorFamilies[i].ProductID, "white", new Array("#FFFFFF"));
		html += GetColorFamilyFormRow(ColorFamilies[i].ProductID, "black", new Array("#000000"));
		html += "</table>";
		document.getElementById("divColorFamilyForm" + ColorFamilies[i].ProductID).innerHTML = html;
		alert(ColorFamilies[i].ProductID);
	}
}
function GetColorFamilyFormRow(pid, color, colorTones) {
	html = "";
	html += "<<tr>td style='background-color:white;text-align:left;'>";
	html += "<input type='checkbox' name='chkColorFamily" + pid + "[]' value='" + color + "' " + (ColorFamily_CheckColor(pid, color) ? " checked" : "") + " /> " + color;
	for (i = 0; i < colorTones.length; i++) {
		html += "<span style='width:50px;background-color:" + colorTones[i] + ";color:" + colorTones[i] + ";letter-spacing:5px;'>..</span>";
	}
	html += "</td></tr>";
	return html;
}
function ShowColorFamilyEditor(obj, e, productId) {
	ShowLayer("divColorFamilyEditor", obj, e, -210, -200);
	document.getElementById("ColorFamilyEditor_txtProductID").value = productId;
	cf = ColorFamily_Get(productId);
	ColorFamilyEditorCount1 = 0;
	html = "<table cellspacing='1' cellpadding='2' border='0' style='background-color:black;'>";
	html += GetColorEditorTableRow("ivory", new Array("#FFFFF0"));
	html += GetColorEditorTableRow("beige", new Array("#F5F5DC"));
	html += GetColorEditorTableRow("wheat", new Array("#F5DEB3"));
	html += GetColorEditorTableRow("tan", new Array("#D2B48C"));
	html += GetColorEditorTableRow("khaki", new Array("#C3B091"));
	html += GetColorEditorTableRow("grey", new Array("#EEEEEE", "#C0C0C0", "#808080", "#464646"), "silver");
	html += GetColorEditorTableRow("blue", new Array("#000080", "#084C9E", "#0000CD", "#007FFF", "#00E5EE"));
	html += GetColorEditorTableRow("green", new Array("#228B22", "#7FFF00", "#BFFF00", "green", "#00CD66", "#90EE90", "#CAFF70"));
	html += GetColorEditorTableRow("red", new Array("#FF0000", "#FF6347", "#CD4F39"));
	html += GetColorEditorTableRow("cyan", new Array("#00FFFF", "aqua", "#7FFFD4"));
	html += GetColorEditorTableRow("teal", new Array("#008080"));
	html += GetColorEditorTableRow("gold", new Array("#FFD700", "#DAA520", "#CDAD00", "#EEC900", "#8B7500"));
	html += GetColorEditorTableRow("pink", new Array("#FC0FC0", "#FF77FF", "pink", "#EEA9B8", "#FF1493"));
	html += GetColorEditorTableRow("purple", new Array("#4B0082", "#843179", "#0000CD", "#007FFF", "#551A8B", "#A020F0"));
	html += GetColorEditorTableRow("yellow", new Array("#FFFF00", "#EEEE00", "#CDCD00", "#8B8B00", "#FFFFE0"));
	html += GetColorEditorTableRow("orange", new Array("#FFA500", "#EE9A00", "#CD8500", "#FF4500"));
	html += GetColorEditorTableRow("brown", new Array("#A52A2A", "#8B2323"));
	html += GetColorEditorTableRow("white", new Array("#FFFFFF"));
	html += GetColorEditorTableRow("black", new Array("#000000"));
	html += "</table>";
	document.getElementById("ColorFamilyEditor_ColorChooser").innerHTML = html;
	for (i =0; i < cf.Colors.length; i++){
		document.getElementById("ColorFamilyEditor_chkColor_" + cf.Colors[i]).checked = "checked";
	}
}
ColorFamilyEditorCount1 = 0;
function GetColorEditorTableRow(color, colorTones) {
	html = "";
	ColorFamilyEditorCount1 = 0;
	if (ColorFamilyEditorCount1 % 3 == 0) html += "<tr>";
	ColorFamilyEditorCount1++;
	html += "<td style='background-color:white;text-align:left;'>";
	html += "<input type='checkbox' id='ColorFamilyEditor_chkColor_" + color + "' name='ColorFamilyEditor_chkColor[]' value='" + color + "' /> " + color;
	for (i = 0; i < colorTones.length; i++) {
		html += "<span style='width:50px;background-color:" + colorTones[i] + ";color:" + colorTones[i] + ";letter-spacing:5px;'>..</span>";
	}
	html += "</td>";
	return html;
}
