///////////////////////////////////////////
////   Quote Functions
/////////////////////////////////////////
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*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 eraseCookie(name) {
	createCookie(name,"",-1);
	setTimeout("location.reload(true);",0);
}
function removeItem(name) {
	alert(name);
}
function updateValues() {
	setTimeout("location.reload(true);",0);
}
function addQuote(sku, description, price, qty, refresh){
	strCookie = sku + "|" + description + "|" + qty + "|" + price;
  	var y = readCookie('quote');
	skuMatch = 0;
	if (y) { 
		y = y.replace(/^\s+|\s+$/g, '') ;	
		// Split on | to get individual values into an array
		var items = y.split('|');		
		// See if the sku already exists
		var i=0;
		while (i < items.length) {
			// If sku matches, update quantity 
			if (sku == items[i]){
				items[i+2]=qty;
				skuMatch = 1;
			}		
			i = i + 4;
		}		
		// Rewrite Cookie
		if (skuMatch == 1){
			var i=1;
			strCookie = items[0];
			while (i < items.length) {
			  strCookie = strCookie + "|" + items[i];
			  i = i + 1; 
			}
		}
		// Append Existing Cookie
		else {
			strCookie = y + "|" + strCookie;	
		}
		createCookie('quote',strCookie,0);
	} 
	else { 
		// Create New Cookie
		createCookie('quote',strCookie,0);
	}	
	// Refresh Quote Page
	if (refresh==1)
	{
			setTimeout("location.reload(true);",0);
	}
}
function addQuoteOption(sku, description, price, qty, refresh){
	QuantityValue= sku + "-Q";
	OptionValue=sku + "-O";
	strCookie = sku + "|" + description + " (Option: " + document.getElementById(OptionValue).value.toUpperCase() + ")" + "|" + document.getElementById(QuantityValue).value + "|" + price;
  	var y = readCookie('quote');
	skuMatch = 0;
	if (y) { 
		y = y.replace(/^\s+|\s+$/g, '') ;	
		// Split on | to get individual values into an array
		var items = y.split('|');		
		// See if the sku already exists
		var i=0;
		while (i < items.length) {
			// If sku matches, update quantity and description (includes option)
			if (sku == items[i]){
				items[i+2]=document.getElementById(QuantityValue).value;
				items[i+1]=description + " (Option: " + document.getElementById(OptionValue).value.toUpperCase() + ")";
				skuMatch = 1;
			}		
			i = i + 4;
		}		
		// Rewrite Cookie
		if (skuMatch == 1){
			var i=1;
			strCookie = items[0];
			while (i < items.length) {
			  strCookie = strCookie + "|" + items[i];
			  i = i + 1; 
			}
		}
		// Append Existing Cookie
		else {
			strCookie = y + "|" + strCookie;	
		}
		createCookie('quote',strCookie,0);
	} 
	else { 
		// Create New Cookie
		createCookie('quote',strCookie,0);
	}	
	// Refresh Quote Page
	if (refresh==1)
	{
			setTimeout("location.reload(true);",0);
	}
}
function displayCookie(name){
	// Table Heading
	document.write('<table border="0" align="center"><tr><td align="right">'); 
	document.write('<form name="formItems">');
	document.write('<table cellspacing="0" cellpadding="1" border="1" width="510">'); 
	document.write('<tr class="quoteform"><td colspan="5" align="center" class="T3">Quote Request Contents</td></tr>'); 
	document.write('<tr class="quoteform">'); 
	document.write('<td nowrap><b>Remove</b></td>'); 
	document.write('<td nowrap><b>VPI Part #</b></td>'); 
	document.write('<td nowrap><b>Description</b></td>'); 
	document.write('<td><b>Qty</b></td>'); 
	document.write('<td><b>List Price (Each)</b></td>'); 
	document.write('</tr>'); 
  	var y = readCookie(name);
  	if (y) {	
		y = y.replace(/^\s+|\s+$/g, '') ;					
		// Split on | to get individual values into an array
		var items = y.split('|');		
		// Get each array value (sku, description, qty, price) and display in table
		var i=0;
		var j=0;
		var itemno = 0;
		var itemsExist = 0;
		while (i < items.length) {
			var item = items[i];
			// Create New Record Cell?
			// Sku
			if (j==0) {
			  // Display if quantity is not equal to zero	
			  if (items[i+2]!=0){	
			  	document.write('<tr class=\"main-text\" onMouseOver="this.bgColor=\'#ffffcc\';" onMouseOut="this.bgColor=\'\';">');
				document.write('<td>');
				document.write('<input type="checkbox" value="0" onClick="addQuote(\'' + items[i] +'\', \'' + items[i+1] + '\', \'' + items[i+2] + '\', \'0\', \'1\');">');
				document.write('</td><td nowrap>');
			    document.write(item);
			    document.write('</td>');				
				itemsExist = 1;
			  }
			}
			// Description
			if (j==1) {
			  // Display if quantity is not equal to zero	
			  if (items[i+1]!=0){	
			    document.write('<td>');
			    document.write(item);
			    document.write('</td>');
			  }
			}
			// Qty
			if (j==2) {
			  // Display if quantity is not equal to zero	
			  if (items[i]!=0){	
			  	document.write('<td>');				  			 
			    document.write('<input name="item1" type="text" value="'+ items[i] +'" size="4" class="box-yellow" onChange="addQuote(\'' + items[i-2] +'\', \'' + items[i-1] + '\', \'' + items[i+1] + '\', this.value, \'0\');">');
			    document.write('</td>');
				itemno = itemno + 1;
			  }
			}	
			// Price	
			if (j==3) { 	
			  // Display if quantity is not equal to zero	
			  if (items[i-1]!=0){	
			    document.write('<td>');
			    document.write(item);
			    document.write('</td>');
				document.write('</tr>');	
			  }					  		  
			}			
		j=j+1;
		// Reset j if end of record
		if (j==4) { 				
			  j=0;
		}
		i=i+1;
		}		
	}
	// Number of Items
	document.write('<input type="hidden" name="itemno" value="'+ itemno +'">');
	// Table Closing
	document.write('</table></td></tr></table></form>');	
	if (itemsExist == 1){
		// Display Update and Clear Contents Button if Items exist
		document.write('<br><input type="button" name="update" value="Update Values" onClick="updateValues()">&nbsp;<input type="button" name="clear" value="Remove All" onClick="eraseCookie(\'quote\')">'); 
	}	
}
function formCookie(name){
	// Table Heading
  	var y = readCookie(name);
  	if (y) {	
		y = y.replace(/^\s+|\s+$/g, '') ;					
		// Split on | to get individual values into an array
		var items = y.split('|');		
		// Get each array value (sku, description, qty, price) and display in table
		var i=0;
		var j=0;
		var itemno = 0;
		var itemsExist = 0;
		while (i < items.length) {
			var item = items[i];
			// Create New Record Cell?
			// Sku
			if (j==0) {
			  // Add if quantity is not equal to zero	
			  if (items[i+2]!=0){	
			  	document.write('<input type="hidden" name="sku'+ itemno+'" value="'+ item +'">');	
				itemsExist = 1;
			  }
			}
			// Description
			if (j==1) {
			  // Add if quantity is not equal to zero	
			  if (items[i+1]!=0){	
			    document.write('<input type="hidden" name="description'+ itemno+'" value="'+ item +'">');			    
			  }
			}
			// Qty
			if (j==2) {
			  // Add if quantity is not equal to zero	
			  if (items[i]!=0){	
			    document.write('<input type="hidden" name="qty'+ itemno+'" value="'+ item +'">');				
			  }
			}	
			// Price	
			if (j==3) { 	
			  // Add if quantity is not equal to zero	
			  if (items[i-1]!=0){	
			    document.write('<input type="hidden" name="price'+ itemno+'" value="'+ item +'">');
				itemno = itemno + 1;
			  }					  		  
			}			
		j=j+1;
		// Reset j if end of record
		if (j==4) { 				
			  j=0;
		}
		i=i+1;
		}		
	}
	// Number of Items
	document.write('<input type="hidden" name="itemno" value="'+ itemno +'">');
}
///////////////////
/// Captcha Code
///////////////////
function ccode(){
wordid = "cword"; 
imagedir = "images/capform/";
wordfield = 20; 
randpic = (Math.floor(Math.random()*10))+1;
imageid = parseInt(randpic);
randomword = 
["alarm","mouse","share","guide","video","power","alert","ports","audio","cable"];
document.write("<table><tr><td><input type=\"text\" id=\"" + wordid + "\" name=\"" + wordid + "\" size=\"" +  wordfield + "\"></td>");
document.write("<td>&nbsp;<img src=\"" + decodeURIComponent(imagedir) + "cap-" + imageid + ".jpg\" width=\"100\" height=\"20\" alt=\"\"></td>");
document.write("<td>&nbsp;<a href=\"#\" onclick=\"playSound('images/capform/cap-"+imageid+".wav');\"><img src=\"images/audio.gif\" border=\"0\" alt=\"Play audio and enter the 5 letter word you hear.\" title=\"Play audio and enter the 5 letter word you hear.\"></a></td></tr></table>");
}
///////////////////
// Form Validation
///////////////////
function FormValidation (FormName, FormNameText) {
  ErrorString = "";
  // Check for Captcha Code 
  cwordentered = document.getElementById(wordid).value;
  cword = cwordentered.toLowerCase();
  if (cword==randomword[randpic-1]) {
  }
  else {
  ErrorString = "Code incorrectly entered, please retry.";	
  }
  // Confirm valid email entered
  if (FormNameText == "formQuote") {
	  AtPos = document.formQuote.email.value.indexOf("@")
	  StopPos = document.formQuote.email.value.lastIndexOf(".")
	  if (AtPos == -1 || StopPos == -1) {
	    ErrorString  = 'The email address entered is not a valid email address.'; 
	  }
	  if (StopPos < AtPos) {
	    ErrorString  = 'The email address entered is not a valid email address.'; 
	  }
	  if (StopPos - AtPos == 1) {
	    ErrorString  = 'The email address entered is not a valid email address.'; 
	  }
	  if (document.formQuote.email.value=="") {
	    ErrorString  = "";
	  }
	  // Check if email address was entered
	  if(document.formQuote.email.value=="") {
	    ErrorString = "Please Enter Your Email Address";
	  }
	  // Check that email address and confirmation email match
	  if(document.formQuote.email.value != document.formQuote.confirmemail.value) {
	    ErrorString = "Email Address and Confirmation Email Address Do Not Match";
	  }
  }
  // Display error is there one, otherwise submit the form
  if (ErrorString != "") {
  	alert (ErrorString);
  }
  if (ErrorString == "") {
    FormName.submit();
  }
}
///////////////////////////////////////////
//// 	Open Quote Page
///////////////////////////////////////////
function openQuote ()
{
	location.href='quote.html?rpage=' + page;
}

///////////////////////////////////////////
////   Option Functions
//////////////////////////////////////////
function optionQty (option, qty)
{
	document.getElementById(option).value=qty;
}