﻿// JScript File

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));
    //alert( (((sign)?'':'-') + '$ ' + num + '.' + cents) );
    return (((sign)?'':'-') + '$' + num + '.' + cents) ; 
}


function calculate(id,val,id2)
{
    var id1="ctl00_MasterFolio_"+ id
    var id2="ctl00_MasterFolio_"+id2

    if(document.getElementById(id1).value != null)
    {
        if(document.getElementById(id1).value * val !="0")
        {
           val = val.substring(2);
          
           //document.getElementById(id2).innerHTML="$ "+(document.getElementById(id1).value * val).toFixed(2);
            //document.getElementById(id2).innerHTML=(document.getElementById(id1).value * val).toFixed(2);
           document.getElementById(id2).innerHTML=formatCurrency(document.getElementById(id1).value * val);
           TotalPrice();  
        }
        else
        {
            document.getElementById(id2).innerHTML="00.00"
            TotalPrice();
        }
    }
    else
    {
	    TotalPrice();
    }
}



function TotalPrice()
{
    var yourTable=document.getElementById("ctl00_MasterFolio_divcontent");
    var yourTotalControl=document.getElementById("ctl00_MasterFolio_totalprice"); //This could be a DIV, SPAN, etc.
    var yourTotalCost=document.getElementById("ctl00_MasterFolio_tdtotalcost");
    
    var grandTotal=0.00;
    
    yourTotalControl.innerHTML ="0.00"; 
    
    for(var i=0;i<yourTable.rows.length;i++) 
    {
        if (yourTable.rows[i].cells.length >= 5)
        {
        var tdid = yourTable.rows[i].cells[4].id;
       //!isNaN(document.getElementById(tdid).innerHTML)&&
        if(document.getElementById(tdid).innerHTML!="")
        {
          //alert(document.getElementById(tdid).innerHTML);    
          var valp= document.getElementById(tdid).innerHTML;
          //valp = valp.substring(2);
          //alert(valp);           
          grandTotal+=parseFloat(valp.toString().replace(/\$|\,/g,''));
          //grandTotal += parseFloat(valp);
          //alert(grandTotal);
          }
            
        }
    }  
    
    if(Math.round(grandTotal.toString()*100)/100 == "0")
    {
        yourTotalControl.innerHTML="0.00";
    }
    else
    {
        //yourTotalControl.innerHTML= "$ " +grandTotal.toFixed(2);
        yourTotalControl.innerHTML=formatCurrency(grandTotal);
        
    }
   //alert(yourTotalControl.innerHTML);
   
   /*var tot;
   if(yourTotalControl.innerHTML!="" && yourTotalControl.innerHTML!="0.00" && yourTotalControl.innerHTML!="0")
   {
       tot= parseFloat(yourTotalControl.innerHTML);
       tot = tot.toFixed(2);
       yourTotalCost.innerHTML = tot;
   }
   else
   {
    tot ="0.00";
    yourTotalCost.innerHTML = tot;
   }*/
   
   TotalQuantity();
   //SubTotal();
}

function TotalQuantity()
{
   var yourTable=document.getElementById("ctl00_MasterFolio_divcontent");
   var yourTotalControl=document.getElementById("ctl00_MasterFolio_totaltickets") //This could be a DIV, SPAN, etc.
   var grandTotal=0.00;
   
    for(var i=0;i<yourTable.rows.length;i++) 
    {
        var tid = "ctl00_MasterFolio_txt" + i;
        showPlayerSearch(tid);
        var obj = document.getElementById(tid);
        if (obj)
        {
            var val = obj.value
            if(val!="")
            {
                grandTotal += parseFloat(document.getElementById(tid).value);
            }
        }
    }
    yourTotalControl.innerHTML= grandTotal; 
}

function SubTotal()
{
    //alert('Hello');
   var yourTable=document.getElementById("ctl00_MasterFolio_divcontent");
   var yourSubTotal=document.getElementById("ctl00_MasterFolio_tdsubtotal") //This could be a DIV, SPAN, etc.
   var yourTotalCost = document.getElementById("ctl00_MasterFolio_totalprice");
   var yourTax = document.getElementById("ctl00_MasterFolio_tdtax");
   var yourConfee = document.getElementById("ctl00_MasterFolio_tdconfee");
   var grandTotal=0.00;
   
   var tot;
   if(yourTotalCost.innerHTML!="" && yourTotalCost.innerHTML!="0.00")
   {
    //tot= parseFloat(yourTotalCost.innerHTML) + parseFloat(yourTax.innerHTML) + parseFloat(yourConfee.innerHTML);

    tot= parseFloat(yourTotalCost.innerHTML) + parseFloat(yourConfee.innerHTML);
    yourSubTotal.innerHTML = tot.toFixed(2);;
   }
   else
   {
    tot ="0.00";
    yourSubTotal.innerHTML = tot;
   }
}

function showPlayerSearch(strID)
{
    var obj = document.getElementById(strID);
    if (obj) 
    {
        var val_1 = obj.value;
    
	    if(trim(document.getElementById(strID).value)== "")
	    {
	        document.getElementById(strID).value="0"
	    }
	}
}
function hidePlayerSearch(strID)
{
	if(trim(document.getElementById(strID).value)== "0")
	{
		document.getElementById(strID).value="";
	}
}
function trim(strg){
	var strr, spos, epos
	strr = ""
	for(var x = 0; x <= strg.length; x++)
	{
		spos = x
		if(!(strg.charAt(x) == ' '))
			break
	}
	if (x = strg.length)
		strr = ""
	for(x = strg.length; x >= 0; x-- )
	{
		epos = x
		if(!(strg.charAt(x) == ' '))
			break
	}
	for(x = spos; x<=epos; x++)
	{
		strr = strr + strg.charAt(x)
	}
	return strr
}

function valbutton() 
{
    if(document.getElementById('ctl00_MasterFolio_dddeliveryMethod').selectedIndex == 0)
    {
        alert('Select a Delivery Method');
        return false;
    }
}

function checkNumeric(evt)
{
    
    var charCode = (evt.which) ? evt.which : event.keyCode
    if((charCode > 32 && charCode < 48) || (charCode > 57 && charCode < 126))
    { 
        alert("Please enter numeric values only...!");        
        return false; 
    }   
    return true;
}
function zeroValidation(tbox)
{
		   var sVal = tbox.value;        
			  
           if (sVal.length == 2)
           {       
               var  sSub= sVal.substring(0, 1);
               if(sSub == 0)
                {   
                   var rSub=sVal.substring(1,2)
                   tbox.value=rSub;                                           
                } 
			}             
}
function valradiobutton() 
{
    // validate myradiobuttons
    myOption = -1;
	for (i=document.aspnetForm.radselect.length-1; i > -1; i--)
	{
		if (document.aspnetForm.radselect[i].checked)
		{
			myOption = i; i = -1;
		}
	}
	if (myOption == -1)
	{
		alert("Please select Delivery Method");
		return false;
	}
}

 function UnloadSess()
{
    var abssize = document.body.offsetWidth-30; 
           
    if (window.event.clientY < 0 && event.clientX >= abssize)  
    {
        //alert('Hello');
        var url ="CheckSession.aspx"; 				    
        xmlhttp=null;	
        if(window.XMLHttpRequest)
        { 
            xmlhttp=new XMLHttpRequest()         
            xmlhttp.onbeforeunload=UnloadSessResults         
            xmlhttp.open("GET",url,true)
            xmlhttp.send(null)
        }
        else if (window.ActiveXObject)
        {      
            try
            {         
                xmlhttp= new ActiveXObject("Msxml2.XMLHTTP.3.0");
            }
            catch(e)
            {
                try
                {            
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
                }
                catch(e){alert("1");}
            }         
            if(xmlhttp)
            {            
                try
                {
                    xmlhttp.abort();                           
                    xmlhttp.onreadystatechange=UnloadSessResults
                    xmlhttp.open("GET",url,false)
                    xmlhttp.send()               
                }
                catch(e){alert("2");}  
            }           
        }
    }
}
function UnloadSessResults()
{
    if(xmlhttp.readyState==4)
    {
        if(xmlhttp.status==200||xmlhttp.status==0)
        {
            //alert('Valid');
        }
        else
        {
            alert(xmlhttp.statusText);
            return false;
        }
    }
}


function onUnloadEvent()
{  
    alert('Hell');
  if(document.frmIndex.hButton.value.length == 0 )
{
           window.open("http://www.hablamosseguros.com/popup_index.php",null,
       "top=200,left=200,height=400,width=600,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes");
}   
else if( document.frmIndex.hButton.value.length == 0 ){  
        window.open("http://www.wespeakinsurance.com/popup_index.php",null,
       "top=200,left=200,height=400,width=600,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes");
    }
   
}

function Tot()
{
    var yourTable=document.getElementById("ctl00_MasterFolio_divcontent");
    var yourSubTot=document.getElementById("ctl00_MasterFolio_lblsubtotal");   
    var yourDelCost=document.getElementById("ctl00_MasterFolio_lbldeliverycost");   
    var yourTotCost = document.getElementById("ctl00_MasterFolio_lbltotcost");  

   //alert(yourSubTot.innerHTML);
   var tot;
   if(yourSubTot.innerHTML!="" && yourSubTot.innerHTML!="0.00")
   {
        //tot = parseFloat(yourSubTot.innerHTML.substring(2)) + parseFloat(yourDelCost.innerHTML.substring(2));
        tot = parseFloat(yourSubTot.innerHTML.toString().replace(/\$|\,/g,'')) + parseFloat(yourDelCost.innerHTML.toString().replace(/\$|\,/g,''));
        
        //alert(tot);
        //yourTotCost.innerHTML = "$ " + tot.toFixed(2);
        yourTotCost.innerHTML=formatCurrency(tot);
   }   
}

function chckdelivery()
{
    alert('Test');
}