/*-----------------------------------------------------------------------
Created by: Shreeya Aryamane
Created date: 22-April-06
File description: Product Details
Special instructions-notes: Java script Validation
Tables used: None
Stored procedures: None
Triggers used: None
-----------------------------------------------------------------------*/

function ShowInfo(page_type,fl_faq,fl_spec,fl_dln)
{
	
	if(fl_spec==1)	
		document.getElementById("div_spec").style.display = 'none';
	if(fl_faq==1)
		document.getElementById("div_faq").style.display = 'none';
	if(fl_dln==1)		
		document.getElementById("div_download").style.display = 'none';
	if(fl_faq==1 || fl_spec==1 || fl_dln==1)	
	document.getElementById(page_type).style.display =  'block';
	
	//Changing Style of tab buttons
	if(fl_spec==1)	
		document.getElementById("but_spec").style.display = 'block';
	if(fl_faq==1)
		document.getElementById("but_faq").style.display = 'block';
	if(fl_dln==1)		
		document.getElementById("but_download").style.display = 'block';
		
	if(fl_spec==1)
		document.getElementById("but_spec_h").style.display = 'none';
	if(fl_faq==1)
		document.getElementById("but_faq_h").style.display = 'none';
	if(fl_dln==1)	
		document.getElementById("but_download_h").style.display = 'none';

	if(fl_faq==1 || fl_spec==1 || fl_dln==1){
	switch(page_type)
	{
		case 'div_spec':
			document.getElementById("but_spec").style.display = 'none';
			document.getElementById("but_spec_h").style.display = 'block';
		  break;    
		case 'div_faq':
			document.getElementById("but_faq").style.display = 'none';
			document.getElementById("but_faq_h").style.display = 'block';
		  break;
		case 'div_download':
			document.getElementById("but_download").style.display = 'none';
			document.getElementById("but_download_h").style.display = 'block';
		  break;
	}
	}
}

function setprice()
{
	totcnt=document.frmStandardModels.totopt.value;	
	aregprice=0;
	asalprice=0;
	newaregprice = 0;
	newasalprice = 0;	
	var newmodel;
	var strnewmodel;
	var instock;
	var strinstock = 0;
	/*	
	if(document.getElementById("txt_quantity").value == "")
	{
		document.getElementById("txt_quantity").value = 0;
	}
	*/	
	if(isNaN(document.getElementById("txt_quantity").value))
	{
		document.getElementById("txt_quantity").value = 1;
	}
	quantity = document.getElementById("txt_quantity").value;
	if((quantity == "") || (quantity == "0"))
	quantity = 1;

	productoriginalregprice=parseFloat(document.frmStandardModels.productoriginalregprice.value);				
    productoriginalsaleprice=parseFloat(document.frmStandardModels.productoriginalsaleprice.value);
	
	if(totcnt>0)
	{
		for(n=1; n<=totcnt; n++)
		{
			optionn = "option"+n;
			optionval = document.frmStandardModels[optionn].value;			
			optionvalarr = optionval.split("##");
			oid = optionvalarr[0];				
			newmodel = optionvalarr[3];
			instock = optionvalarr[4];		
			if(instock == 1){			
				strinstock = instock;
			}
			
			if(newmodel !='') 
			{
			strnewmodel =newmodel;
			}
			if(isNaN(parseFloat(optionvalarr[1])))
			{		
			newaregprice = 0;
			newaregprice = parseFloat(newaregprice);
			}
			else
			 {
			  newaregprice = parseFloat(optionvalarr[1]);
			 }
			if(isNaN(parseFloat(optionvalarr[2])))
			{
			newasalprice = 0;
			newasalprice = parseFloat(newasalprice);
			}
			else
			{
			newasalprice = parseFloat(optionvalarr[2]);				
			}			
			aregprice=aregprice+newaregprice;
			asalprice=asalprice+newasalprice;				
			//aregprice=aregprice+parseFloat(optionvalarr[1]);
			//asalprice=asalprice+parseFloat(optionvalarr[2]);		
			

		}			
			
		if(isNaN(aregprice))
		{		
			aregprice = 0;
			aregprice = parseFloat(aregprice);
		}
		if(isNaN(asalprice))
		{
			asalprice = 0;
			asalprice = parseFloat(asalprice);
		}	
		
		if(strinstock =='0') 
		{			  
			document.getElementById("instock").innerHTML= 'In Stock';	
			document.frmStandardModels.new_out_of_stock_flg.value = '0';								
			document.getElementById("display_addtocart").style.display='';
		    document.getElementById("div_in_stock").style.display = 'block';
		    document.getElementById("div_out_of_stock").style.display = 'none';			
		}
		
		if(strinstock =='1') 
		{
		  document.getElementById("instock").innerHTML = 'Out Stock';	
		  document.frmStandardModels.new_out_of_stock_flg.value = '1';
		  document.getElementById("display_addtocart").style.display='none';
		  document.getElementById("div_in_stock").style.display = 'none';
		  document.getElementById("div_out_of_stock").style.display = 'block';
		}		
		productoriginalregprice=productoriginalregprice+aregprice;			
		productoriginalregprice=roundOff(productoriginalregprice,2);			
	
		productoriginalsaleprice=productoriginalsaleprice+asalprice;
		productoriginalsaleprice=roundOff(productoriginalsaleprice,2);			
		document.frmStandardModels.newproductregprice.value= productoriginalregprice;
		document.frmStandardModels.newproductsaleprice.value= productoriginalsaleprice;			
	}	
	document.getElementById("regprice").innerHTML = "$ " + roundOff((productoriginalregprice*quantity),2);
	document.getElementById("saleprice").innerHTML = "$ " + roundOff((productoriginalsaleprice*quantity),2);	
	document.frmStandardModels.our_price.value = productoriginalsaleprice;		
}
setprice();

function roundOff(value, precision)
{
      var result = "0.00";
      precision = parseInt(precision);          // make sure roundoff is an int
      // alerts are only for debugging and expanation -- they may be deleted/commented
      //alert("Value before rounding is "+value)
      value = ""+Math.round(value * Math.pow(10, precision));  // take the integer part of value*10**precision
      //alert("Value is "+value+"\nLength of value is "+value.length+"\nPrecision is "+precision)
      // determine where to put decimal point
      if (value.length > precision)        // more digits in value than in precision (e.g., 999.99)
      {
         result = value.substring(0, value.length-precision);
         if (precision > 0 ) result += ".";
         result  += value.substring(value.length-precision, value.length);
      }
      else                        // number of digits <= precision and value less than 1.0)
      {
         result = "0.";
         if (value.length < precision)
         {
            for (var i = 0; i < precision - value.length; i++)
            {
               result += "0";                // pad fraction with leading zeros
            }
         }
         result += value;
      }                // end else
		return result;
}

 function addtocart()
  {  
 	  var tmp_str ; 
	  var select_flag;
	  var str_select_flag =0;
	  var tmp_quantity;
	  var charpos;
	  charpos = document.frmStandardModels.txt_quantity.value.search("[^0-9]"); 
	 // alert(document.frmStandardModels.out_of_stock.value);
	  var out_stock = document.getElementById("out_of_stock").value;
	  /*alert(out_stock);
	  alert("In add to cart");*/
	  if(out_stock != 1)
	  {
	   if(document.frmStandardModels.totopt.value >0)
		{
		 for(i=1;i<=document.frmStandardModels.totopt.value;i++) 
		  { 
			tmp_str = "option"+i;		
			 tmp_quantity  = document.frmStandardModels[tmp_str].value;
		
			//if(isWhitespace(document.frmStandardModels[tmp_str].value))
			
			//tmp_quantity  = document.getElementById(tmp_str).value;
			tmp_quantity  =document.frmStandardModels[tmp_str].value;
			 
			 if(tmp_quantity =='Select' || tmp_quantity =='' )
			  {		  
			   select_flag=0;
			   str_select_flag=str_select_flag+1;
			  }
			  else
			   {
			   select_flag=1;		    
			   }	   
		  } 	  
		}
	  }
	  else
	  {
	  	 alert('The product is out of stock');
	  	 return false; 
	  }		
	 if(str_select_flag>0)
	  {	   
	   alert('Please select Product Option(s).');
	   return false; 
	  }
	  else if((document.frmStandardModels.txt_quantity.value == "") || (document.frmStandardModels.txt_quantity.value == "0"))
	  {
	  		alert("Please enter quantity");
			document.frmStandardModels.txt_quantity.focus();
			return false; 
	  }
	  else if(document.frmStandardModels.txt_quantity.value.length > 0 &&  charpos >= 0) 
	  {
			alert("Please enter numeric value");
			document.frmStandardModels.txt_quantity.focus();
			return false; 			
	  }
	 else
	  {
		document.frmStandardModels.add_to_cart.value = 1; 
		document.frmStandardModels.action = 'add_to_cart.php';
		document.frmStandardModels.submit();  
		return true;
	   }
  }   
  
function changeImage(imageName)
{
	var image_path;
	image_path = document.getElementById("add_image_path").value;
	document.getElementById("display_image").src = image_path+imageName;	
}

function setOriginal()
{
	var imageName;
	imageName =  document.getElementById("Original_Image").value;
	document.getElementById("display_image").src = imageName;	
}
