
/**************************************************************************************************************/
/*                                      ModuleName: ApplicationScript                                         */  
/*                          Description: It contains all the javascripts that are used in different web pages */
/*                                       of the web application
/**************************************************************************************************************/


//Global Variables
           
var totalval = "";               //This variable stores total value for the selected  Product

var Item_Code = "";              //This variable stores product code of the selected  Product 

var textval = "";                //This variable stores quantity of selected  Product t be purchased

var isSubmited = true;          //Checks whether the form is to be submitted or not

//This function removrs the blank spaces from the left and right of the string
function alltrim(str) 
{
	return str.replace(/^\s+|\s+$/g, '');
}

/****************************************************************
This method returns a boolean variable which determines whether 
the form will be submitted or not.
*****************************************************************/

function isSubmit(oForm)
 {
	if(!isSubmited){
		return false;
	}
	else
	{
	    return true;
	}
 }
 
/*********************************************************************************************
When the web page is loaded ,at that time this method  is executed price field of the   
selected product is updated. Hidden parameters are initialized with the Item Name, Item Code,
quantiry of item to be purchased and the total amount.
**********************************************************************************************/
function pageonload()
{
   	var rad_val = 0;
    var item_value = "";
    if(document.form1.isProductAvailable.value == "true")
    {
   	     textval = alltrim(document.form1.txtQuantity.value);
   	    if(document.form1.Radio1.length > 1)
   	    {
   	         for (var i=0; i < document.form1.Radio1.length; i++)
            {
		        if (document.form1.Radio1[i].checked)
		        {
			        rad_val = document.form1.Radio1[i].value;
			        item_value = document.form1.Radio1[i].title;
			        Item_Code = document.form1.Radio1[i].id;
			        break;
		        }
            }
   	    }
   	    else
   	    {
            rad_val = document.form1.Radio1.value;
	        item_value = document.form1.Radio1.title;
	        Item_Code = document.form1.Radio1.id;
        }
	    totalval = parseFloat(textval * rad_val);
	    document.getElementById("_lblTotal").innerHTML = totalval;
	    document.getElementById("amount").value = rad_val;
	    document.getElementById("item_name").value = item_value;
	    document.getElementById("quantity").value = textval;
	    document.getElementById("item_number").value = Item_Code;
	}
	        
}
/*************************************************************************************************************
/*    Method Name = quantity123(Text1)
/*    Parameter = Text1 :The value of the quantity text box
/*    Description : When the quantity field is changed this methos is invoked. It updated the total
/*                  price filed by multiplying the product unit price and the ubit of the quantity
/*                  entered by the  user. Quantity value should be non zero intiger value.
*************************************************************************************************************/
function quantity123(Text1)
{
    var rad_val;
    var item_value;
    var qty = 0;
    var unitval =0;
    textval = alltrim(document.form1.txtQuantity.value);
    if( document.form1.Radio1.length > 1)
    {
        for (var i=0; i < document.form1.Radio1.length; i++)
        {
		    if (document.form1.Radio1[i].checked)
		    {
			    rad_val = document.form1.Radio1[i].value;
			    item_value = document.form1.Radio1[i].title;
			    Item_Code = document.form1.Radio1[i].id;
			    break;
		    }
        }
    }
    else
    {
        rad_val = document.form1.Radio1.value;
        item_value = document.form1.Radio1.title;
        Item_Code = document.form1.Radio1.id;
    }
    var rex = "^[0-9]*$";
   
    if (Text1.match(rex))
    {
        if(textval != "")
        {
            qty = parseFloat(textval);
            unitval = parseFloat(rad_val);
        }
        totalval = totalval = qty * unitval;;
        totalval = Math.round(totalval*Math.pow(10,2))/Math.pow(10,2);
        document.getElementById("_lblTotal").innerHTML = totalval;
        document.getElementById("amount").value = rad_val;
        document.getElementById("item_name").value = item_value;
        document.getElementById("quantity").value = textval;
        document.getElementById("item_number").value = Item_Code;
        
    }
    else
    {
       alert("Only numbers may be entered here");
       document.form1.txtQuantity.value = "";
       document.form1.txtQuantity.focus();
       document.getElementById("_lblTotal").innerHTML = 0;
    }
}

/*********************************************************************************************
When buyer select one product at that time this method is called.
Depending upon the selection the total price field is updated by multiplying the selected 
product unit price with the number of units of the product to be purchased
**********************************************************************************************/
function radVal(singVal)
{
    var rad_val;
    var item_value;
    textval = alltrim(document.form1.txtQuantity.value);
    if( document.form1.Radio1.length > 1)
    {
        for (var i=0; i < document.form1.Radio1.length; i++)
        {
		    if (document.form1.Radio1[i].checked)
		    {
			    rad_val = document.form1.Radio1[i].value;
			    item_value = document.form1.Radio1[i].title;
			    Item_Code = document.form1.Radio1[i].id;
			    break;
		    }
        }
    }
    else
    {
        rad_val = document.form1.Radio1.value;
        item_value = document.form1.Radio1.title;
        Item_Code = document.form1.Radio1.id;
    }
    var rex = "^[0-9]*$";
    if (textval.match(rex))
    {
        totalval= (textval * rad_val);
        totalval = Math.round(totalval*Math.pow(10,2))/Math.pow(10,2);
        document.getElementById("_lblTotal").innerHTML = totalval;
        document.getElementById("amount").value = rad_val;
        document.getElementById("item_name").value = item_value;
        document.getElementById("quantity").value = textval;
        document.getElementById("item_number").value = Item_Code;      
    }
    else
    {
       alert("Only numbers may be entered here.");
       document.form1.txtQuantity.value = "";
       document.form1.txtQuantity.focus();
       document.getElementById("_lblTotal").innerHTML = 0;
    }
    
    
    
}
/*********************************************************************************************
When buyer select number of quantity and click enter at that time this method is called.
This method do the following things
                    1: Validates the text inputs and shows an error message if validation fails
                    2: calculate the total price of the selected product and transfer the control 
                        to the order summary page on successfull validation
**********************************************************************************************/
function SubmitBuyLicense(e)
{
    var eventInstance = window.event ? event : e;
    var unicode = eventInstance.charCode ? eventInstance.charCode : eventInstance.keyCode;

    if (unicode == 13)
    {
        var qty = document.getElementById("txtQuantity").value;
        var pattern = "^[0-9]*$";
        if(qty.match(pattern))
        {
        	if(qty == 0)
        	{
	            alert("Please enter a non-zero quantity");
	            document.form1.txtQuantity.value = "";
	            document.form1.txtQuantity.focus();
	            document.getElementById("_lblTotal").innerHTML = 0;
	            isSubmited = false;
	            return false;
        	}
        	else 
        	{
        	    document.getElementById("quantity").value = qty;
	        	document.form1.action = "http://ecom.softtrends.com/DispatchOrder.aspx?ProductId="+Item_Code+"&Quantity="+qty;
				document.form1.method = "post";
				document.form1.submit();
			}
		}
		else
		{
			alert("Only numbers may be entered here.");
			document.form1.txtQuantity.value = "";
        	document.form1.txtQuantity.focus();
        	document.getElementById("_lblTotal").innerHTML = 0;
        	isSubmited = false;
        	return false;
      	}
    }
}
/*********************************************************************************************
When buyer select CHECK OUT button at that time this method call.
Here take product quantity value.
Quantity value should be non zero intiger value. And going to next page "DispatchOrder.aspx".
**********************************************************************************************/
function CheckOutProduct()
{
    var _txtQuantitry = alltrim(document.getElementById("txtQuantity").value);
    var _regExp = "^[0-9]+$";
    if((!_txtQuantitry.match(_regExp)) || (document.getElementById("txtQuantity").value == 0))
    {
        if(document.getElementById("txtQuantity").value == 0)
        {
            alert("Please enter a non-zero quantity.");
        }
        else
        {
            alert("Only numbers may be entered here.");
        }
        document.getElementById("txtQuantity").value = "";
        document.form1.txtQuantity.focus();
        document.getElementById("_lblTotal").innerHTML = 0;
        return;
    }
    
    document.getElementById("quantity").value = _txtQuantitry;
    document.form1.action = "http://ecom.softtrends.com/DispatchOrder.aspx?ProductId="+Item_Code+"&Quantity="+_txtQuantitry;
    document.form1.method = "post";
    document.form1.submit();
    
}

/*********************************************************************************************
    Method Name:SubmitBuyUpgrades
    Description: This method validated the txt quantity entered by the user in the buy upgrades
    page and if validation success it transfer the control to the dispatch order page

**********************************************************************************************/

function SubmitBuyUpgrades(e)
{
    var eventInstance = window.event ? event : e;
    var unicode = eventInstance.charCode ? eventInstance.charCode : eventInstance.keyCode;
  
    if (unicode == 13)
    {
        BuyUpgrades();
    }
    else
    {
        isSubmited = true;
    }
}


function BuyUpgrades()
{
        var qty = document.getElementById("_txtQuantity").value;
        var maxqty =document.getElementById("PurchasedQuantity").value;
        
        
        //Validates the qty
        if( alltrim(qty) == "")
        {
            alert("Quantity field can not be blank");
            document.frmBuyUpgrades._txtQuantity.focus();
            return;
        }
        
        var pattern = "^[0-9]*$";
        if(qty.match(pattern))
        {
            if(parseInt(qty,10) == 0)
            {
                alert("Please enter a non-zero quantity");
                document.frmBuyUpgrades._txtQuantity.value = "";
                document.frmBuyUpgrades._txtQuantity.focus();
                isSubmited = false;
                return false;
            }
            else if(parseInt(qty,10) >parseInt(maxqty,10))
            {
                alert("Upgrade quantity can not be greater than purchsed quantity");
                document.frmBuyUpgrades._txtQuantity.value = "";
                document.frmBuyUpgrades._txtQuantity.focus();
                isSubmited = false;
                return false; 
            }
            else 
            {
                isSubmited = true;
                document.getElementById("quantity").value = qty;
                document.frmBuyUpgrades.action = "DispatchOrder.aspx";
			    document.frmBuyUpgrades.method = "post";
			    document.frmBuyUpgrades.submit();
		    }
		}
		else
		{
			alert("Only numbers may be entered here.");
			document.frmBuyUpgrades._txtQuantity.value = "";
            document.frmBuyUpgrades._txtQuantity.focus();
            isSubmited = false;
            return false;
        }
}

