// JavaScript Document
$formNumber = 0;
$machines = new Object();
$formBackup = new Array();
$pass = false;

// value - text
function start( $formNum )
{
	$machines["quant"+$formNum] = 1;
	$machines["access"+$formNum+"price"] = "";
	$machines["access"+$formNum+"partNum"] = "";
	$machines["access"+$formNum+"text"] = "";
}

function loadAccessories( $accessID, $modelID, $formNum )
{
	$modelTag = document.getElementById($modelID);
	
	$.post("quoteInt.php", {model: $modelTag.value, act: "accessOp"},function(data){
	  //alert("Data Loaded: " + data);
	 ops = data.split("\n");
	 document.getElementById($accessID).length = 0;
	 for($i = 0;$i<ops.length-1;++$i)
	 {
		 $details = ops[$i].split("^");
		 appendOptionLast( $details[1], $details[0], $accessID );
	 }
	});
	modelChange( $modelID, $formNum );
}

function appendOptionLast( $text, $value, $accessID )
{
  var elOptNew = document.createElement('option');
  elOptNew.text = $text;
  elOptNew.value = $value;
  var elSel = document.getElementById($accessID);

  try {
    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    elSel.add(elOptNew); // IE only
  }
}

function addForm( $addID )
{
	++$formNumber;
	document.getElementById($addID).style.visibility='hidden';
	
	var $form = '    		<div class="quoteForm' + $formNumber + '">'+
                '<div class="quoteCol1">'+
                    'Select Model<br /><br />'+
                    '<select name="model'+$formNumber+'" id="model'+$formNumber+'" size="1" onchange="loadAccessories(\'accessories'+$formNumber+'\',\'model'+$formNumber+'\','+$formNumber+' )" onchange="quanityChange( \'quantity'+$formNumber+'\', '+$formNumber+' )">'+
                    	'<?php $formLoader->loadModelSelectBox(); ?>'+
                    '</select>'+
                '</div>'+
                '<div class="quoteCol3">'+
                    'Quantity<br /><br />'+
                    '<input name="quantity'+$formNumber+'" type="text" value="1" size="2" maxlength="2" />'+
                '</div>'+
                '<div class="quoteCol2">'+
                    'Select Accessories<br />'+
                    '(Shift + click to select multiple accessories)<br />'+
                    '<select class="accessories" name="accessories'+$formNumber+'[]" size="11" multiple="multiple" id="accessories'+$formNumber+'" onchange="accessChange( \'accessories'+$formNumber+'\', '+$formNumber+' )"></select>'+
                '</div>'+
                '<div class="quoteClear">'+
				//'<span onclick="removeMachine('+$formNumber+')"><img src="Minus.jpg" width="24" height="24" alt="Remove Machine" />Remove Quote<br /></span>'+
              '<span id="Add'+$formNumber+'" onclick="addForm(\'Add'+$formNumber+'\')"><img src="Plus.jpg" width="24" height="24" alt="Add Machine" /><span style=" position:relative; top:-3px; left:3px;">Add another machine</span></span>'+
              '</div>'+
			'</div>';
			
			//Save form State
			saveFormState();
			document.getElementById("forms").innerHTML += $form;
			//Restore Form State
			restoreFormState();
			
	start( $formNumber );
	
	$.post("quoteInt.php", {act: "modelOp"},function(data){
	  //alert("Data Loaded: " + data);
	 ops = data.split("\n");
	
	 for($i = 0;$i<ops.length-1;++$i)
	 {
		 $details = ops[$i].split("^");
		 appendOptionLast( $details[1], $details[0],"model"+$formNumber );
	 }
	});

	$Sum =             '<div class="MachineSummary" id="machineSummary'+$formNumber+'"><br /></div>'+
'            <div class="textbreaker" id="textbreaker'+$formNumber+'"></div>';

	document.getElementById("summary").innerHTML += $Sum;
}

function quanityChange( $quantID, $formNum )
{
	$newQuant = document.getElementById($quantID).value;
	
	if(isNumeric($newQuant))
	{
		//alert("enter quantityChange");
		$machines["quant"+$formNum] = $newQuant;
		//alert("Mid quantityChagne");
		updateSum( $formNum );
		//alert("end Quantity Change");
	}
	else
	{
		alert("You must enter a number for Quantity");	
	}
}

function accessChange( $accessID, $formNum )
{
	$accessTag = document.getElementById($accessID);
	//alert($accessID);
	$machines["access"+$formNum+"price"] = "";
	$machines["access"+$formNum+"partNum"] = "";
	$machines["access"+$formNum+"text"] = "";
	
	for (var i=$accessTag.options.length-1; i >= 0;--i) 
	{
		//alert($accessTag.options[i].text);
		if ($accessTag.options[i].selected) 
		{
			$pp = $accessTag.options[i].value.split('-');
			$machines["access"+$formNum+"price"] += $pp[1] + "-";
			$machines["access"+$formNum+"partNum"] += $pp[0 + "-"];
			$machines["access"+$formNum+"text"] += $accessTag.options[i].text + "-";
		}
	}
	//alert($machines["access"+$formNum+"text"]);
	updateSum($formNum);
}

function modelChange( $modelID, $formNum )
{
	$modelTag = document.getElementById($modelID);
	$pp = ($modelTag.value).split("-");
	$machines["model"+$formNum+"price"] = $pp[1];
	$machines["model"+$formNum+"partNum"] = $pp[0];
	$machines["model"+$formNum+"text"] = $modelTag.options[$modelTag.selectedIndex].text;
	//alert("model Change text:" + $modelTag.options[$modelTag.selectedIndex].text + ":" + $modelTag.value );
	
	updateSum( $formNum );
}

function updateSum( $formNum )
{
	//alert("updateSum");
	$SumTag = document.getElementById("machineSummary" + $formNum)
	$sum = " " + $machines["quant"+$formNum] + " x " + $machines["model"+$formNum+"text"] + " ";
	
	//$aPrices = $machines["access"+$formNum+"price"].split("-");
	//$aPartNums = $machines["access"+$formNum+"partNum"].split("-");
	$aTexts = $machines["access"+$formNum+"text"].split("-");
	
	for(var i=0;i<$aTexts.length-1;++i)
	{
		$sum += "+ " + $aTexts[i] + " ";	
	}
	
	$SumTag.innerHTML = $sum;
}

function noSubmit()
{
	//alert("no Submit");
	//Do nothing
	if($pass)
		return true;
	else
		return false;
}

function saveFormState()
{//alert("Start Save From");
	$formTag = document.getElementById("machine");
	
	for($i=0;$i<$formTag.elements.length;++$i)
	{
		//alert("in Save From");
		$formBackup[$i] = $formTag.elements[$i];
	}
	//alert("End Save From");
}

function restoreFormState()
{	//alert("Start restore From "+$formBackup.length);
	$formTag = document.getElementById("machine");
	
	for($i=0;$i<$formBackup.length;++$i)
	{
		//alert("In Restore From " + $formBackup[$i].type);
		$formTag.elements[$i] =$formBackup[$i];
		switch($formBackup[$i].type)
		{
		case "text": restoreText($formTag.elements[$i], $formBackup[$i] ); break;
		case "select-one": restoreSelOne($formTag.elements[$i], $formBackup[$i] ); break;//
		case "select-multiple": restoreSelMult($formTag.elements[$i], $formBackup[$i] ); break
		}
	}
	
	$formBackup = new Array;
	//alert("End restore From");
}

function restoreText($formElement, $backup)
{
	$formElement.value = $backup.value;
}

function restoreSelOne($formElement, $backup )
{
	$formElement.selectedIndex = $backup.selectedIndex;
}

function restoreSelMult($formElement, $backup)
{
	for (var i=0;i<$backup.options.length;++i) 
	{
		//alert($accessTag.options[i].text);
		if ($backup.options[i].selected) 
		{
			$formElement.options[i].selected = true;
		}
	}
}

function sendQuote()
{
	$pass = validate();
	if($pass)
		 document.machine.submit();
}

function validate()
{
	valid = true;
	errorMsg = "";
	
	if( document.getElementById("email").value == "you@yourEmailAdrress.com" )
	{
		valid = false;
		errorMsg += "You must enter an Email addrress\n";
	}
	
	if(!valid)
		alert(errorMsg);
	
	return valid;
}

function isNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}


function removeMachine($num)
{	
	document.getElementById('quoteForm'+$num.toString()).innerHTML = "";alert("After Clear InnerHTML");
	document.getElementById('quoteForm'+($num-1).toString()).style.visibility='visible';
}
