﻿
//check if data is a number
function checknumber(testData)
{
   var anum=/(^\d+$)|(^\d+\.\d+$)/
    if (anum.test(testData))
           return true;
    else
           return false;
}

//transfer currency to number format  
function CurrencyToNum(num) 
{	
   num = num.toString().replace(/\$|\,/g,'');
 
   if(isNaN(num))
      num = "0";
   
   num = Math.floor(num*100+0.50000000001);
   cents = num%100;
   num = Math.floor(num/100);
   if (cents>0)
      {
        num = num +'.'+cents;
        var newNum = new Number(num);
        newNum = newNum.toFixed(2);
      }
    else
      var newNum = num;  
 
   return newNum; 
}


//transfer number to the format of currency
function formatOfCurrency(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);
    
}

//get the element ID  
function getID(id)
{
  return document.getElementById(id);
}

//compute total quantity, amount of the product 
function compute_total(personIDflag,personID,flag,qtyID,originalpriceID,directpriceID,totalID,qtyTotID,gtotafterID,rows)
{	
 var subtotalBefore=0;
 var subtotalAfter=0;
 var totalFormat="";
 var priceFormat= "";
 var numPrice=0;
 var directprice=0;
 var personselect="";
 var personalize="NO";

 //if quantity is in input
 if (flag=="1") 
   var qty=1*getID(qtyID).value; 
 else 
   var qty=parseInt(getID(qtyID).innerHTML); 
   
 var gtotalNum=0;
 var qtyTotNum=0;
 var row=0; 
 var numOfRows=rows;
 
 //check if is a number
 if (!checknumber(qty))
     {
	   alert("Please Enter Valid Quantity!");
	   if (flag=="1")
	      getID(qtyID).value="0";
	   getID(totalID).innerHTML="$0.00";
	   	 
      }     
  else 
    {       
      var originalprice=getID(originalpriceID).innerHTML;
      numPrice = CurrencyToNum(originalprice);
      numPrice = parseFloat(numPrice);
      subtotalBefore = 1*qty*numPrice;    
     
      if (qty!=0)
       {
         directprice = numPrice;
	     subtotalAfter = subtotalBefore;
        }
       else
         {
           directprice = 0 ;
           subtotalAfter = 0;
          }
	   
	  //if there is a "personalize" input selection   
	  if (personIDflag=="1")
	     {
	       personselect=getID(personID);
	       personalize=personselect.options[personselect.selectedIndex].value;
         }
         
	  if (personalize!="YES") 
	     {       
	       if (subtotalBefore == 0)
  	        {
  	           directprice = 0;
  	           subtotalAfter = 0;
  	         } 
           if (subtotalBefore>0)
            { 
	          if (subtotalBefore<=1000)       
                {                   
                  directprice = numPrice;
  	              subtotalAfter = subtotalBefore;
                 }
               else
                 if (subtotalBefore<=2999)    
                  {       
	                 directprice = numPrice*0.9;
	                 subtotalAfter = subtotalBefore*0.9;
                   } 
                  else
	                if (subtotalBefore<=4999)
	                  {
                         directprice = numPrice*0.85;
                         subtotalAfter = subtotalBefore*0.85; 
                       }   
                     else
	                   if (subtotalBefore<=19999)
	                    {
	                       directprice = numPrice*0.8;
                           subtotalAfter = subtotalBefore*0.8;   
	                      }
	                    else
                          {  
	                         directprice = numPrice*0.80;
                             subtotalAfter = subtotalBefore*0.8;   
	                         alert("You may qualify for additional discount, please call the customer service!");
                           }
            }  
        }       
        
        if (qty>=0)
          { //direct price                 
            priceFormat = formatOfCurrency(directprice);
            getID(directpriceID).innerHTML = priceFormat;
            //subtotal         
            totalFormat = formatOfCurrency(subtotalAfter);
            getID(totalID).innerHTML = totalFormat; 
          }                        
   }  
  
 //caculate quantity total and product grand total
 var rowqtyID="";
 var rowtotalID="";
 var numSubtotal=0;
 var subtotal="";
 var discount=0;
 var discountFormat="";
 var gtotalAfterNum=0;
 var qtyNum=0;
  
 for (row=0; row < numOfRows; row++)
   { 
      rowqtyID = "qty"+row;
      if (flag=="1")
         qty=1*getID(rowqtyID).value;
      else  
         qty=getID(rowqtyID).innerHTML;   
      
      qtyTotNum = parseInt(qtyTotNum) + parseInt(qty); 
       
      rowtotalID = "total"+row;
      subtotal = getID(rowtotalID).innerHTML;
      numSubtotal = CurrencyToNum(subtotal);
      gtotalNum = parseFloat(gtotalNum) + parseFloat(numSubtotal);
    
    }  
    
 totalFormat=formatOfCurrency(gtotalNum);
 //product grand total
 getID(gtotafterID).innerHTML = totalFormat; 
 //total quantity 
 getID(qtyTotID).innerHTML = qtyTotNum;  
                                                             
 } 
 
//check if the fields(number of recipients and budget) are numbers
function checkNumBudget()
{ 
  var alertMsg = "Please enter valid numbers for the following fields:  \n"
  var l_Msg = alertMsg.length;
  //number of recipients
  var recipients = trim(getID("recipients").value);
  if (!checknumber(recipients))
      alertMsg += "  - Number of recipients\n";
  //Total gift budget
  var budget = trim(getID("budget").value);
  if (!checknumber(budget))
      alertMsg += "  - Total gift budget\n";
  if (alertMsg.length == l_Msg)
	  { 
	  	 //change form post action	
	     document.forms[0].action="/main/gift_finder/find_gifts.aspx";   
         document.forms[0].submit();
	   }
	   
	 else 
       { 
		   alert(alertMsg); 
	       return false;
	   }
  
}

//check if the quantity field is a number       
function checkquantity(qtyID)
{
  var qty=getID(qtyID).value; 
  
  if (!checknumber(qty))
     {
	   alert("Please Enter Valid Quantity!");
       getID(qtyID).value="0"; 
      }     
}

//check if all the quantity fields are empty
function Qtyfieldscheck(rows)
{ 
  var i = 0;
  var empty = "1";
  var qty = "";
  var qtyId = ""; 
 
  for (i=0; i<rows; i++)
   {
	  qtyId ="qty"+i; 
	  qty = trim(getID(qtyId).value);
	 
	  if (qty != "0")
	     empty = "0";
     }
     
  if (empty == "1")
    {
	 // alert("Please input the QUANTIY!") 
     var answer=confirm("You haven't input anything in the QUANTITY fields yet, Would you like to continue?");
     if (answer)
	     document.forms[0].action="/main/request_quote/quote_request.aspx";  
      else  
	     document.forms[0].action="/main/idea_business/idea_business.aspx";
     }
   else  
     document.forms[0].action="/main/request_quote/quote_request.aspx";   
        
  document.forms[0].submit();     
}    	     


//trim spaces from string
function trim(stringValue)
{
  return stringValue.replace(/(^\s*|\s*$)/, "");
}
 

//check the required fields
function Fieldscheck()
{
    //check for security 
    r = trim(getID("r").value);
	r2 = trim(getID("sec_code").value);
	if(!isValid(r,r2))
	{ 
	    alert("Security code mismatch");
	    return false;
	}	
	
	var firstname = "",lastname = "",phone= "",email = "", seccode = "";
	var alertMsg = "Please complete the following fields:  \n";
	var l_Msg = alertMsg.length;
		
	// first name
	firstname = trim(getID("firstId").value);
	if (firstname == "")  
	  { 
	    alertMsg += "  - First Name\n";
	   }
	
	// last name
	lastname = trim(getID("lastId").value);
	if (lastname == "")  
	  { 
	    alertMsg += "  - Last Name\n";
	   }

	// Phone number
	phone = trim(getID("phoneId").value);
	if (phone == "")  
	  { 
	    alertMsg += "  - Phone\n";
	   }
	 
	// email address
	email = trim(getID("emailId").value);
	if (email == "")  
	  { 
	    alertMsg += "  - E-mail \n";
	   }
	
	if (alertMsg.length == l_Msg)
	  { 
	  	 //change form post action	
	     document.forms[0].action="/main/request_quote/request_thanks.aspx";   
	     document.forms[0].submit();
	   }
	   
	 else 
       { 
		   alert(alertMsg); 
	       return false;
	   }
    
}

 
function Boxescheck(flag)
{
    //check "ADD To BASKET"
   
	if (getID("check1").checked==false) 
	    getID("qty1").value = "";
	    
	if (getID("check2").checked==false)
	    getID("qty2").value = "";    

	if (getID("check3").checked==false)
	    getID("qty3").value = "";    
	   
	if (getID("check4").checked==false)
        getID("qty4").value = "";
	    
    if (getID("check5").checked==false)
	    getID("qty5").value = "";    

	if (getID("check6").checked==false)
	    getID("qty6").value = "";    
	   
	if (getID("check7").checked==false)
	    getID("qty7").value = "";
	    
    if (getID("check8").checked==false)
	    getID("qty8").value = "";    

	if (getID("check9").checked==false)
	    getID("qty9").value = "";    
	      
	if (flag=="1")
	    document.forms[0].action="/main/idea_business/business_quote.aspx"; 
	
	if (flag=="2")
	    document.forms[0].action="/main/idea_business/business_order.aspx"; 
	                           	       
	document.forms[0].submit();

}