/**
	Author: Markus Stocker, modified by FAU.
	Date: 08/24/2005

	PLEASE NOTE: the array order for the images MUST be the same
	as defined on table shop_colors
**/

// Index of the allImages array
tileColors = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
          	       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);

// Index of the images array
tileClicks = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
          	       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);

var images = new Array();
var tempImages = new Array();
var countSelectedColors = 0;

// 1 tile = 2.54 square feet (19.125" per side)
// 1 square foot = 1.00 $
var squareFeetPerTile = 2.54;
var designerNrOfTiles = 324;
// Taxes in percent
var taxes = 0.0;
var mouseX = 0;
var mouseY = 0;

// Variables used for image popup
var DHTML=0,DOM=0,MS=0,NS=0,OP=0;
if(window.opera) { OP=1; }
if(document.getElementById) { DHTML=1; DOM=1; }
if(document.all && !OP) { DHTML=1; MS=1; }
if(window.netscape && window.screen && !DOM && !OP) { DHTML=1; NS=1; }

// Error messages
var PATTERN_ERROR = "Please select a pattern";
var SQUARE_FEET_ERROR = "Please enter square feet";
var NR_OF_TILES_ERROR = "Please enter number of tiles for each color";
var SELECT_COLOR_ERROR = "Please select one or more color(s) first";
var DESIGN_FLOOR_ERROR = "Please design your floor first";
var COLOR_ERROR = "Please select your color";
var LINEAL_FEET_ERROR = "Please enter linear feet";

// Capter mouse move
if (!MS) document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = getMouseXY;

// Screen height
var screenH = screen.height;

function checkCCForm()
{
	var creditCardNumber = document.getElementById('cc_number').value;
	var creditCardExp = document.getElementById('cc_exp').value;
	var cvv = document.getElementById('cvv').value;
	
	if (creditCardNumber.indexOf(".") >= 0 || creditCardNumber.length == 0 || (! isFinite(creditCardNumber))) {
		alert("Wrong format for credit card number");
		document.getElementById('cc_number').focus();
		return false;
	} else if (creditCardExp.indexOf(".") >= 0 || creditCardExp.length != 4 || (! isFinite(creditCardExp))) {
		alert("Wrong format for expiration date");
		document.getElementById('cc_exp').focus();
		return false;
	} else if (cvv.indexOf(".") >= 0 || (! isFinite(cvv)) || (cvv.length > 0 && (cvv.length < 3 || cvv.length > 4))) {
		alert("Wrong format for cardholder verification value");
		return false;
	}

	if (cvv.length > 0) {
		document.getElementById('cvv_indicator').value = "present";
	}
	
	return true;
}

function cvvIndicatorSelection()
{
	var cvvIndicator = document.getElementById('cvv_indicator').value;

	if (cvvIndicator == "present") {
		document.getElementById('cvv').focus();
	} else {
		document.getElementById('cvv').value = "";
	}
}

function sampleRequestUpdateColors(pattern)
{
	var colors = new Array('black','red','blue','taupe','teal','light_gray','dark_gray','white','canvas');

	for (i = 0; i < 9; i++) {
		image = new Image();
		image.src = '/img/a1_prime_' + pattern + '_' + colors[i] + '_small.jpg';
		document.getElementById('color'+i).src = image.src;
	}
}

function getMouseXY(e)
{
	if (! e) e = window.event;

	mouseX = e.screenX;
	mouseY = e.screenY;
	
	if (mouseX < 0) mouseX = 0;
	if (mouseY < 0) mouseY = 0;

	return true;	
}

// On designer loading init the arrays depending which pattern was choosed
function initImageArrays(pattern)
{
	var img = new Array();
	
	images.push('img/a1_prime_' + pattern + '_black_designer.jpg');
	images.push('img/a1_prime_' + pattern + '_red_designer.jpg');
	images.push('img/a1_prime_' + pattern + '_blue_designer.jpg');
	images.push('img/a1_prime_' + pattern + '_taupe_designer.jpg');
	images.push('img/a1_prime_' + pattern + '_teal_designer.jpg');
	images.push('img/a1_prime_' + pattern + '_light_gray_designer.jpg');
	images.push('img/a1_prime_' + pattern + '_dark_gray_designer.jpg');
	images.push('img/a1_prime_' + pattern + '_white_designer.jpg');
	images.push('img/a1_prime_' + pattern + '_canvas_designer.jpg');

	tempImages.push('img/none_30x30.jpg');

	for (i = 0; i < images.length; i++) {
		img[i] = new Image();
		img[i].src = images[i];
	}
}

function checkIfColorIsSelected()
{
	if (countSelectedColors > 0) {
		return true; 
	} else {
		alert(SELECT_COLOR_ERROR);
		return false;
	}
}

function getAbsoluteIndexOfColor(colorName)
{
	// Returns the array index (images) of the color (path name)
	// The colors MUST be in the same order as defined in table shop_colors!!
	var index = 0;

	for (i = 0; i < images.length; i++) {
		if (images[i] == colorName) {
			// Color on shop_colors are numbered from 1..n
			index = i + 1;
		}
	}

	return index;
}

function SwapImage(imgNr)
{
	var relativePathToImage;
	var nextImage = new Image();
	var nrOfTiles = 0;
	var pricePerTile = parseFloat(document.getElementById("primePricePerTile").value);

	if (checkIfColorIsSelected() == true) {
		relativePathToImage = tempImages[tileClicks[imgNr]];
		nextImage.src = relativePathToImage;
		document.getElementById('tile'+imgNr).src = nextImage.src;
	
		if (tempImages.length == tileClicks[imgNr] + 1) { 
			tileClicks[imgNr] = 0;
			tileColors[imgNr] = 0;
		} else {
			tileClicks[imgNr]++; 
			tileColors[imgNr] = getAbsoluteIndexOfColor(relativePathToImage);
		}
	
		for (i = 0; i < 400; i++) {
			if (tileColors[i] > 0) {
				nrOfTiles++;
			}
		}
		// pricePerTile was replaced with 9.37
		// squareFeetPerTile was replaced with 2.54 (1.59" x 1.59")
		price = nrOfTiles * 9.37;
		sf = nrOfTiles * 2.54;
	
		// Double precision
		price = formatCurrency(price);
       		sf = makeDoublePrecision(sf);
	
		document.getElementById('img'+imgNr).value = tileColors[imgNr];
		document.getElementById("infoNrTiles").innerHTML = nrOfTiles;
		document.getElementById("infoSquareFeet").innerHTML = sf;
		document.getElementById("infoPrice").innerHTML = price;
	}
}	

function pushColor()
{
	var color = 0;
  	var countTempImages = tempImages.length;

	// Delete clicks array
	for (i = 0; i < designerNrOfTiles; i++) {
		tileClicks[i] = 0;
	}
	
	// Delete colors array
	for (i = 0; i < designerNrOfTiles; i++) {
		tileColors[i] = 0;
	}
	
  	// Delete images array
	for (i = 0; i < countTempImages; i++) {
    		tempImages.pop();
		countSelectedColors = 0;
	}

	// Add the colors to the image array 
	for (i = 0; i < images.length; i++) {
		color = i + 1;
		if (document.getElementsByName("color" + color)[0].checked == true) {
			tempImages.push(images[i]);
			countSelectedColors++;
   		}
  	}

	// Read colors array from form
	for (i = 0; i < designerNrOfTiles; i++) {
		tileColors[i] = document.getElementById('img'+i).value;
	}
	
	tempImages.push('img/none_30x30.jpg');
}


function addTaxes()
{
	var costTaxes, stringCostTaxes, costWithTaxes, costNoTaxes, stringCostWithTaxes, stringCostNoTaxes;
	var subTotal = document.shippingForm.subTotal.value;
	var discount = document.shippingForm.discount.value;
	var freight = document.shippingForm.freight.value;
 	var total = document.shippingForm.total.value;

	if (document.getElementById("100").checked == true) {
		costTaxes = (parseFloat(subTotal) / 100) * taxes;
		costWithTaxes = parseFloat(subTotal) + costTaxes + parseFloat(freight) - parseFloat(discount);
		stringCostTaxes = formatCurrency(costTaxes);
		stringCostWithTaxes = formatCurrency(costWithTaxes);
		
		document.shippingForm.taxes.value = costTaxes;
		document.shippingForm.total.value = costWithTaxes;
		document.getElementById("taxes").innerHTML = stringCostTaxes;
		document.getElementById("total").innerHTML = stringCostWithTaxes;
	} else if (document.getElementById("100").checked == false) {
		costTaxes = document.shippingForm.taxes.value;
		costNoTaxes = parseFloat(total) - parseFloat(costTaxes);
		stringCostTaxes = formatCurrency(costTaxes);
		stringCostNoTaxes = formatCurrency(costNoTaxes);

		document.shippingForm.taxes.value = "0";
		document.shippingForm.total.value = costNoTaxes;
		document.getElementById("taxes").innerHTML = "0.00";
		document.getElementById("total").innerHTML = stringCostNoTaxes;
		
	}
}

function makeDoublePrecision(doubleValue)
{
	return String(doubleValue).substring(0,String(doubleValue).indexOf('.')+3);
}

function checkRequestSampleForm()
{
	if (document.getElementsByName("firstname")[0].value == "") {
		alert("Please enter your first name");
		document.getElementsByName("firstname")[0].focus();
		return false;
	} else if (document.getElementsByName("lastname")[0].value == "") {
		alert("Please enter your last name");
		document.getElementsByName("lastname")[0].focus();
		return false;
	} else if (document.getElementsByName("address")[0].value == "") {
		alert("Please enter your address");
		document.getElementsByName("address")[0].focus();
		return false;
	} else if (document.getElementsByName("city")[0].value == "") {
		alert("Please enter your city");
		document.getElementsByName("city")[0].focus();
		return false;
	} else if (document.getElementsByName("state")[0].value == "") {
		alert("Please enter your state");
		document.getElementsByName("state")[0].focus();
		return false;
	} else if (document.getElementsByName("zip")[0].value == "") {
		alert("Please enter your ZIP/Postal Code");
		document.getElementsByName("zip")[0].focus();
		return false;
	} else if (document.getElementsByName("country")[0].value == "") {
		alert("Please enter your country");
		document.getElementsByName("country")[0].focus();
		return false;
	} else if (document.getElementsByName("email")[0].value == "") {
		alert("Please enter your email");
		document.getElementsByName("email")[0].focus();
		return false;
	} else if (! document.getElementsByName("email")[0].value.match(/(.+)(\@)(.+)(\.)(\w.+)/)) {
		alert("Please enter a valid email");
		document.getElementsByName("email")[0].focus();
		return false;
	} else if (document.getElementsByName("phone")[0].value == "") {
		alert("Please enter your phone");
		document.getElementsByName("phone")[0].focus();
		return false;
	} else {
		return true;
	}
}

function openWindow(theURL,winName,features)
{
  window.open(theURL,winName,features);
}

function formatCurrency(num) 
{
	num = num.toString().replace(/\$|\,/g,'');
	
	if(isNaN(num)) num = "0";

	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10) cents = "0" + cents;

	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
	}
	
	return (((sign)?'':'-') + num + '.' + cents);
}

// Function used for image popup
function showImage(img, top, topCorrection)
{
    var basePathToImages = "";
   
    // Set the popup top style
    if (topCorrection == 1 && mouseY < screenH / 2 + 80) { 
	top += 420;
    }
    document.getElementById("Popup").style.top = top + "px";
    
    var t = '<table width="270" height="240" border="0" cellspacing="4" cellpadding="0">'
    + '<tr>'
    + '<td align="left" valign="top">'
    + '<img src="'+basePathToImages+'/'+img+'" class="popup">'
    + '</td>'
    + '<td align="right" valign="top">'
    + '<input type="button" class="btn" onClick="hide();return false;" value="X">'
    + '</td></tr>'
    + '</table>';

    if(document.documentElement && document.documentElement.scrollTop>=0)
    {
	document.getElementById("Popup").innerHTML=t;
	document.getElementById("Popup").style.visibility="visible";
	//absX=relX;
	//absY=document.documentElement.scrollTop+relY;
    }
    else if(document.body && document.body.scrollTop>=0)
    {
	document.getElementById("Popup").innerHTML=t;
	document.getElementById("Popup").style.visibility="visible";
	//absX=relX;
	//absY=document.body.scrollTop+relY;
    }
    else if(window.pageYOffset>=0)
    {
	document.getElementById("Popup").document.write(t);
	document.getElementById("Popup").document.close();
	document.getElementById("Popup").visibility="show";
	//absX=relX;
	//absY=window.pageYOffset+relY;
    }
    else
    {
	//absX=relX;
	//absY=relY;
    }

    //document.getElementById("Popup").style.left=absX;
    //document.getElementById("Popup").style.top=absY;
}

// Function used for image popup
function hide()
{
    document.getElementById("Popup").style.visibility = 'hidden';
/**
    popup=getElem("id","Popup",null);
    img=0;

    if(MS)
    {
	popup.style.visibility='hidden';
	popup.innerHTML='';
    }
    else if(DOM)
    {
	popup.style.visibility='hidden';
	popup.style.top=-500;
    }
    else if(NS)
    {
	popup.visibility='hide';
	popup.top=-500;
    }
**/
}

// Function used for image popup
function getElem(p1,p2,p3)
{
    var Elem;
    if(DOM)
    {
	if(p1.toLowerCase()=="id")
	{
    	    if (typeof document.getElementById(p2) == "object") Elem=document.getElementById(p2);
	    else Elem = void(0);
	    return(Elem);
	}
	else if(p1.toLowerCase()=="name")
	{
	    if (typeof document.getElementsByName(p2) == "object") Elem=document.getElementsByName(p2)[p3];
	    else Elem = void(0);
	    return(Elem);
	}
	else if(p1.toLowerCase()=="tagname")
	{
	    if (typeof document.getElementsByTagName(p2) == "object" || (OP && typeof document.getElementsByTagName(p2) == "function")) Elem=document.getElementsByTagName(p2)[p3];
	    else Elem = void(0);
	    return(Elem);
	}
	else return void(0);
    }
    else if(MS)
    {
	if(p1.toLowerCase()=="id")
	{
	    if (typeof document.all[p2] == "object") Elem=document.all[p2];
	    else Elem=void(0);
	    return(Elem);
	}
	else if(p1.toLowerCase()=="tagname")
	{
	    if (typeof document.all.tags(p2) == "object") Elem=document.all.tags(p2)[p3];
	    else Elem=void(0);
	    return(Elem);
	}
	else if(p1.toLowerCase()=="name")
	{
	    if (typeof document[p2] == "object") Elem=document[p2];
	    else Elem=void(0);
	    return(Elem);
	}
	else return void(0);
    }
    else if(NS)
    {
	if(p1.toLowerCase()=="id" || p1.toLowerCase()=="name")
	{
	    if (typeof document[p2] == "object") Elem=document[p2];
	    else Elem=void(0);
	    return(Elem);
	}
	else if(p1.toLowerCase()=="index")
	{
	    if(typeof document.layers[p2] == "object") Elem=document.layers[p2];
	    else Elem=void(0);
	    return(Elem);
	}
	else return void(0);
    }
}







