﻿// JScript File

function formValidate(oSrc, args)
{
    var objBillNew=document.getElementById("radioBillNew");
    var flag=null;
    var flag1=null;
    var flag2=null;
    if(objBillNew!=null)
    {
        if(objBillNew.checked)
        {
            var formobj=document.forms["formBillShip"];
            if(formobj)
            {
                // Enter name of mandatory fields
	            // Enter field description to appear in the dialog box
	            var fieldRequired = Array("billFName", "billLName", "billAddress1", "billZip", "billCountry", "hdnBillCountryName", "billState", "hdnBillStateName", "billCity", "billPhNo", "billEmail");
	            // Enter field description to appear in the dialog box
	            var fieldDescription = Array("Billing First Name", "Billing Last Name", "Billing Address", "Billing Zip Code", "Billing Country", "Billing Country", "Billing State", "Billing State", "Billing City", "Billing Phone Number", "Billing Email Id");
	            // Dialog message
	            var alertMsg = "Please complete the following fields:\n";
	            var l_Msg = alertMsg.length;
	            for(var i=0;i<fieldRequired.length;i++)
	            {
	                var obj = formobj.elements[fieldRequired[i]];
	                if(obj)
	                {
	                    switch(obj.type)
	                    {
	                        case "select-one":			            
			                    if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == "" || obj.options[obj.selectedIndex].value == "0")
			                    {
				                    alertMsg += " - " + fieldDescription[i] + "\n";
				                    obj.focus();
			                    }				    	    
				                break;
			                case "select-multiple":
				                if (obj.selectedIndex == -1)
				                {
					                alertMsg += " - " + fieldDescription[i] + "\n";
					                obj.focus();
				                }
				                break;
				            case "text":
			                    if (obj.value == "" || obj.value == null)
				                {
					                alertMsg += " - " + fieldDescription[i] + "\n";
					                obj.focus();
				                }
				                else
				                {
				                    // Checking Email Address Starts
	                                if(obj.name=="billEmail") 
	                                {
	                                    if(!validateEmail(obj.value))
	                                    {
	                                        if(obj.name=="billEmail")
	                                        {
	                                            alertMsg += " - Valid Billing Email address.\n";
	                                            obj.focus();
	                                        }
	                                    }
	                                }
	                                // Checking Email Address Ends
    				            
		                            // Checking Telephone Number Starts
		                            if(obj.name=="billPhNo") 
		                            {
		                                if(!validatePhNo(obj.value))
		                                {
		                                    if(obj.name=="billPhNo")
		                                    {
		                                        alertMsg += " - Valid Billing Phone Number.\n";
		                                        obj.focus();
		                                    }			                    
		                                }
		                            }           
		                            // Checking Telephone Number Ends
		                            
		                            // Checking Zip Code Starts
		                            if(obj.name=="billZip") 
		                            {
		                                if(!validateZipCode(obj.value))
		                                {
		                                    if(obj.name=="billZip")
		                                    {
		                                        alertMsg += " - Valid Billing Postal Code.\n";
		                                        obj.focus();
		                                    }			                    
		                                }
		                            }           
		                          // Checking Zip Code Ends   
				                }
				                break;
				                
				        case "textarea":
				                if (obj.value == "" || obj.value == null)
				                {
					                alertMsg += " - " + fieldDescription[i] + "\n";
					                obj.focus();
				                }
				                break;
				        case "select":
			                    if (obj.value == "" || obj.value == null)
			                    {	
				                    alertMsg += " - " + fieldDescription[i] + "\n";
				                    obj.focus();
			                    }
				                break;
        				    
        				    
			            default:
	                    }
	                    if (obj.type == undefined)
			            {
				            var blnchecked = false;
				            for (var j = 0; j < obj.length; j++)
				            {
					            if (obj[j].checked)
					            {
						            blnchecked = true;
					            }
				            }
				            if (!blnchecked)
				            {
					            alertMsg += " - " + fieldDescription[i] + "\n";
				            }
			            }
	                }
	            }
	            if (alertMsg.length == l_Msg)
	            {
		            //return true;
		            args.IsValid=true;
		            flag=args.IsValid;
	            }
	            else
	            {
		            alert(alertMsg);
		            //return false;
		            args.IsValid=false;
		            flag=args.IsValid;
	            }
            }
            else
	        {
	            alert("Form object not found.");
	            args.IsValid=false;
	        }
        }
    }
    
        var objShipExist=document.getElementById("radioShipAddBook");
        //alert(objShipExist);
        if(objShipExist!=null)
        {
            if(objShipExist.checked)
            {
                var formobj=document.forms["formBillShip"];
                if(formobj)
                {
                    var fieldRequired = Array("shipDate","ddlshipname");
                    var fieldDescription= Array("Shipping Date"," Choose Existing Shipping Address from Dropdown");
                    var alertMsg = "Please complete the following field:\n";
	                var l_Msg = alertMsg.length;
	                for (var i = 0; i < fieldRequired.length; i++)
	                {
	                    var obj = formobj.elements[fieldRequired[i]];
	                    if(obj)
	                    {
	                        switch(obj.type)
	                        {
	                            case "select-one":			            
			            
			                    if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == "" || obj.options[obj.selectedIndex].value == "0")
			                    {			                    
				                    alertMsg += " - " + fieldDescription[i] + "\n";
				                    alert(alertMsg);
				                    
				                    obj.focus();
				                  //  return false;
				                   args.IsValid=false;
			                    }				    	    
				                break;
				                case "text":
			                    if (obj.value == "" || obj.value == null)
				                {
					                alertMsg += " - " + fieldDescription[i] + "\n";
					                obj.focus();
				                }
				                else
				                {
				                    // Checking Ship Date
		                            if(obj.name=="shipDate")
		                            {
		                                if(!validateShipDate(obj.value))
		                                {
		                                    if(obj.name=="shipDate")
		                                    {
		                                        alertMsg += " - Valid Shipping Date.\n";
		                                        obj.focus();
		                                        
		                                    }
		                                }
		                            }
		                            // Checking Shipping Date End 
    	
				                }
				                break;
				                default:
	                        }
	                    }
	                }
	                if (alertMsg.length == l_Msg)
	                {
		                //return true;
		                args.IsValid=true;
		                flag1=args.IsValid;
	                }
	                else
	                {
		                alert(alertMsg);
		                //return false;
		                args.IsValid=false;
		                flag1=args.IsValid;
	                }
                }
            }
        }
       
       var objShipNew=document.getElementById("radioNewShip"); 
       if(objShipNew!=null)
       {
            if(objShipNew.checked)
            {
                var formobj=document.forms["formBillShip"];
                if(formobj)
                {
                    // Enter name of mandatory fields
	                // Enter field description to appear in the dialog box
                    var fieldRequired = Array("shipDate", "shipFName", "shipLName", "shipAddress1", "shipZip", "shipCountry", "shipState", "hdnShipStateName", "shipCity", "hdnShipCityName", "shipEmail", "shipPhNo");
	                // Enter field description to appear in the dialog box
	                var fieldDescription = Array("Shipping Date", "Shipping First Name", "Shipping Last Name", "Shipping Address", "Shipping Zip Number", "Shipping Country", "Shipping State", "Shipping State", "Shipping City", "Shipping City", "Shipping Email","Shipping Phone Number");
	                // Dialog message
	                var alertMsg = "Please complete the following fields:\n";
	                var l_Msg = alertMsg.length;
	                
	                for (var i = 0; i < fieldRequired.length; i++)
	                {
	                    var obj = formobj.elements[fieldRequired[i]];
	                    if (obj)
		                {
		                    switch(obj.type)
		                    {
		                        case "select-one":			            
			                    if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == "" || obj.options[obj.selectedIndex].value == "0")
			                    {
				                    alertMsg += " - " + fieldDescription[i] + "\n";
				                    obj.focus();
			                    }				    	    
				                break;
			                    case "select-multiple":
				                if (obj.selectedIndex == -1)
				                {
					                alertMsg += " - " + fieldDescription[i] + "\n";
					                obj.focus();
				                }
				                break;

				            case "text":
				                if (obj.value == "" || obj.value == null) {
				                    if (obj.name != "shipEmail") {
				                        alertMsg += " - " + fieldDescription[i] + "\n";
                                    }
				                    
				                    obj.focus();
				                }
				                else {
				                    // Checking Email Address Starts
				                    if (obj.name == "shipEmail") {

				                        if (obj.value != '' && obj.value != null) {

				                            if (!validateEmail(obj.value)) {
				                                if (obj.name == "shipEmail") {
				                                    alertMsg += " - Valid Shipping Email address.\n";
				                                    obj.focus();
				                                }
				                                else if (obj.name == "shipReEmail") {
				                                    alertMsg += " - Valid Shipping Email address.\n";
				                                    obj.focus();
				                                }
				                            }
				                        }


				                    }
				                    // Checking Email Address Ends

				                    // Checking Telephone Number Starts
				                    if (obj.name == "shipPhNo") {
				                        if (!validatePhNo(obj.value)) {
				                            if (obj.name == "shipPhNo") {
				                                alertMsg += " - Valid Shipping Phone Number.\n";
				                                obj.focus();
				                            }
				                        }
				                    }
				                    // Checking Telephone Number Ends

				                    // Checking Zip Code Starts
				                    if (obj.name == "shipZip") {
				                        if (!validateZipCode(obj.value)) {
				                            if (obj.name == "shipZip") {
				                                alertMsg += " - Valid Shipping Postal Code.\n";
				                                obj.focus();
				                            }
				                        }
				                    }
				                    // Checking Zip Code Ends 

				                    // Checking Ship Date
				                    if (obj.name == "shipDate") {
				                        if (!validateShipDate(obj.value)) {
				                            if (obj.name == "shipDate") {
				                                alertMsg += " - Valid Shipping Date.\n";
				                                obj.focus();
				                            }
				                        }
				                    }
				                    // Checking Shipping Date End 

				                }
				                break;
				                case "textarea":
				                if (obj.value == "" || obj.value == null)
				                {
					                alertMsg += " - " + fieldDescription[i] + "\n";
					                obj.focus();
				                }
				                break;
				                case "select":
			                    if (obj.value == "" || obj.value == null)
			                    {	
				                    alertMsg += " - " + fieldDescription[i] + "\n";
				                    obj.focus();
			                    }
				                break;


				            default:
				                break;
		                    }
		                    if (obj.type == undefined) {
		                        var blnchecked = false;
		                        for (var j = 0; j < obj.length; j++) {
		                            if (obj[j].checked) {
		                                blnchecked = true;
		                            }
		                        }
		                        if (!blnchecked) {
		                            alertMsg += " - " + fieldDescription[i] + "\n";
		                        }
		                    }
		                    
		                }
	                }
	                if (alertMsg.length == l_Msg)
	                {
		                //return true;
		                args.IsValid=true;
		               flag2=args.IsValid;
	                }
	                else
	                {
		                alert(alertMsg);
		                //return false;
		                args.IsValid=false;
		                flag2=args.IsValid;
	                }
                }
                else
	            {
	                alert("Form object not found.");
	                args.IsValid=false;
	            }
            }
       } 
       if(flag1==true)
       {
            
            args.IsValid=true;
       }
       if(flag==true && flag1==true)
       {
            
            args.IsValid=true;
       }
       if(flag==true && flag1==false)
       {
            
            args.IsValid=false;
       }
       if(flag1==true && flag==false)
       {
            
            args.IsValid=false;
       }
       if(flag==true && flag2==true)
       {
            
            args.IsValid=true;
       }
       if(flag2==true && flag==false)
       {
            
            args.IsValid=false;
       }
       if(flag==true && flag2==false)
       {
           
            args.IsValid=false;
       }
       
       
      /* if(!flag)
       {
            args.IsValid=false;
       }
       if(!flag1)
       {
            args.IsValid=false;
       }
       else
       {
            alert('hi');
            args.IsValid=true;       
       }
       if(!flag2)
       {
            args.IsValid=false;
       }
       
       if(flag && flag2)
       {
            //alert('hi');
            args.IsValid=true;
       } 
       if(flag && flag1)
       {
           // alert('hi');
            args.IsValid=true;
       } 
       if(!flag && flag1)
       {
           // alert('hi');
            args.IsValid=false;
       } */
       
       //alert(flag1);
       
}

function formValidate1(oSrc, args)
{
    var objRegisterUser=document.getElementById("radioRegisterUser");
    
    if(objRegisterUser!=null)
    {
        if(objRegisterUser.checked)
        {
            var formobj=document.forms["formBillShip"];
            if(formobj)
            {
                var fieldRequired = Array("RegisterEmail","RegisterPassword");
                var fieldDescription= Array("Enter Email Id","Enter Password");
                var alertMsg = "Please complete the following fields:\n";
	            var l_Msg = alertMsg.length;
	            
	            for(var i=0;i<fieldRequired.length;i++)
	            {
	                
	                var obj = formobj.elements[fieldRequired[i]];
	                  
	                
	                if(obj)
	                {
	                    switch(obj.type)
	                    {
	                        case "text":
			                    if (obj.value == "" || obj.value == null)
				                {
				                    
					                alertMsg += " - " + fieldDescription[i] + "\n";
					                obj.focus();
				                }
				                else
				                {
				                    // Checking Email Address Starts
	                                if(obj.name=="RegisterEmail") 
	                                {
	                                    if(!validateEmail(obj.value))
	                                    {
	                                        if(obj.name=="RegisterEmail")
	                                        {
	                                            alertMsg += " - Valid Email address.\n";
	                                            obj.focus();
	                                        }
	                                    }
	                                }
	                                // Checking Email Address Ends
	                                
	                                if(obj.name=="RegisterPassword")
	                                {
	                                    alertMsg += " - Enter password.\n";
	                                    obj.focus();
	                                }
	                            } 
	                            break;
	                        case "password":
			                    if (obj.value == "" || obj.value == null)
				                {
				                    
					                alertMsg += " - " + fieldDescription[i] + "\n";
					                obj.focus();
				                }
				                break;
        				        default: 
        				        
        				          
	                    }
	                }
	                
	            }
	            if (alertMsg.length == l_Msg)
                {
	                //return true;
	                args.IsValid=true;
                }
                else
                {
	                alert(alertMsg);
	                //return false;
	                args.IsValid=false;
                }
	                  
            }
            else
            {
              alert("Form object not found.");
                args.IsValid=false;  
            }  
        
        }
    }
    
    var objNewUser=document.getElementById("radioNewUser");
    
    if(objNewUser!=null)
    {
        if(objNewUser.checked)
        {
            var formobj=document.forms["formBillShip"];
            if(formobj)
            {
                // Enter name of mandatory fields
	            // Enter field description to appear in the dialog box
	            var fieldRequired = Array("NewEmail","NewPassword","NewRePassword","billFName", "billLName", "billAddress1", "billZip", "billCountry", "hdnBillCountryName", "billState", "hdnBillStateName", "billCity", "billPhNo", "billEmail");
	            // Enter field description to appear in the dialog box
	            var fieldDescription = Array("Email Id","Password","Re-type Password","Billing First Name", "Billing Last Name", "Billing Address", "Billing Zip Code", "Billing Country", "Billing Country", "Billing State", "Billing State", "Billing City", "Billing Phone Number", "Billing Email Id");
	            // Dialog message
	            var alertMsg = "Please complete the following fields:\n";
	            var l_Msg = alertMsg.length;
	            for(var i=0;i<fieldRequired.length;i++)
	            {
	                var obj = formobj.elements[fieldRequired[i]];
	                if(obj)
	                {
	                    switch(obj.type)
	                    {
	                        case "select-one":			            
			                    if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == "" || obj.options[obj.selectedIndex].value == "0")
			                    {
				                    alertMsg += " - " + fieldDescription[i] + "\n";
				                    obj.focus();
			                    }				    	    
				                break;
			                case "select-multiple":
				                if (obj.selectedIndex == -1)
				                {
					                alertMsg += " - " + fieldDescription[i] + "\n";
					                obj.focus();
				                }
				                break;
				            case "text":
			                    if (obj.value == "" || obj.value == null)
				                {
					                alertMsg += " - " + fieldDescription[i] + "\n";
					                obj.focus();
				                }
				                else
				                {
				                    // Checking Email Address Starts
	                                if(obj.name=="billEmail") 
	                                {
	                                    if(!validateEmail(obj.value))
	                                    {
	                                        if(obj.name=="billEmail")
	                                        {
	                                            alertMsg += " - Valid Billing Email address.\n";
	                                            obj.focus();
	                                        }
	                                    }
	                                }
	                                if(obj.name=="NewEmail") 
	                                {
	                                    if(!validateEmail(obj.value))
	                                    {
	                                        if(obj.name=="NewEmail")
	                                        {
	                                            alertMsg += " - Valid Email address.\n";
	                                            obj.focus();
	                                        }
	                                    }
	                                }
	                                
	                                // Checking Email Address Ends
    				            
		                            // Checking Telephone Number Starts
		                            if(obj.name=="billPhNo") 
		                            {
		                                if(!validatePhNo(obj.value))
		                                {
		                                    if(obj.name=="billPhNo")
		                                    {
		                                        alertMsg += " - Valid Billing Phone Number.\n";
		                                        obj.focus();
		                                    }			                    
		                                }
		                            }           
		                            // Checking Telephone Number Ends
		                            
		                            // Checking Zip Code Starts
		                            if(obj.name=="billZip") 
		                            {
		                                if(!validateZipCode(obj.value))
		                                {
		                                    if(obj.name=="billZip")
		                                    {
		                                        alertMsg += " - Valid Billing Postal Code.\n";
		                                        obj.focus();
		                                    }			                    
		                                }
		                            }           
		                          // Checking Zip Code Ends   
				                }
				                break;
				                
				        case "textarea":
				                if (obj.value == "" || obj.value == null)
				                {
					                alertMsg += " - " + fieldDescription[i] + "\n";
					                obj.focus();
				                }
				                break;
				        case "select":
			                    if (obj.value == "" || obj.value == null)
			                    {	
				                    alertMsg += " - " + fieldDescription[i] + "\n";
				                    obj.focus();
			                    }
			                    break;
			            case "password":
			                    if (obj.value == "" || obj.value == null)
				                {
				                    
					                alertMsg += " - " + fieldDescription[i] + "\n";
					                obj.focus();
				                }
				                else
                                {
                                    if(obj.value=="NewPassword" || obj.value=="NewRePassword")
                                    {
                                        if(obj.name=="NewPassword")
	                                    {
	                                        alertMsg += " - Valid Password.\n";
	                                        obj.focus();
	                                    }
	                                    else if(obj.name=="NewRePassword")
	                                    {
	                                        alertMsg += " - Valid Re-Password.\n";
	                                        obj.focus();
	                                    }	
                                        else(!stringMatch('NewPassword','NewRePassword'))
                                        {				                    
                                            alertMsg += " - Please check both the password are not same.\n";
                                        }
                                    }
                                }
				                break;
				                
        				    
        				    
			            default:
	                    }
	                    if (obj.type == undefined)
			            {
				            var blnchecked = false;
				            for (var j = 0; j < obj.length; j++)
				            {
					            if (obj[j].checked)
					            {
						            blnchecked = true;
					            }
				            }
				            if (!blnchecked)
				            {
					            alertMsg += " - " + fieldDescription[i] + "\n";
				            }
			            }
	                }
	            }
	            if (alertMsg.length == l_Msg)
	            {
		            //return true;
		            args.IsValid=true;
	            }
	            else
	            {
		            alert(alertMsg);
		            //return false;
		            args.IsValid=false;
	            }
            }
            else
	        {
	            alert("Form object not found.");
	            args.IsValid=false;
	        }
        
        }
    
    }
}

function IsNumeric(e)
{
    // Calling procedure
    // onKeyPress=\"javascript:return IsNumeric(event);\" 
    //
    //alert(e);
	var KeyID = (window.event) ? event.keyCode : e.which;
	if((KeyID >= 66 && KeyID <= 90) || (KeyID >= 97 && KeyID <= 122) || (KeyID >= 33 && KeyID <= 47) ||
	   (KeyID >= 58 && KeyID <= 64) || (KeyID >= 91 && KeyID <= 96) || (KeyID >= 123 && KeyID <= 126) || (KeyID == 32))
	{
		return false;
	}
    return true;
}



function validatePhNo(str)
{
    if(str)
    {
        //if(isNaN(str) || str.length<6 || str.length>15)
        if(str.length<6 || str.length>15)
        {
            return false;
        }
        else
        {
            return true;
        }    
    }
    else
    {
        return false;
    }
}

function validateZipCode(str)
{
    if(str)
    {
        if(str.length<4 || str.length>10)
        {
            return false;
        }
        else
        {
            return true;
        }    
    }
    else
    {
        return false;
    }
}
function validateEmail(str)
{
    if(str)
    {
        var at="@"
        var dot = "."
        var ex = "!";
        var lat=str.indexOf(at)
        var lstr=str.length
        var ldot=str.indexOf(dot)
        if (str.indexOf(at)==-1)
        {
           //alert("Invalid E-mail ID")
           return false;
        }
        if(str.indexOf(ex) != -1) {

            return false;   
        }
        if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
        {
           //alert("Invalid E-mail ID")
           return false;
        }

        if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
        {
            //alert("Invalid E-mail ID")
            return false;
        }

         if (str.indexOf(at,(lat+1))!=-1)
         {
            //alert("Invalid E-mail ID")
            return false;
         }

         if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
         {
            //alert("Invalid E-mail ID")
            return false;
         }

         if (str.indexOf(dot,(lat+2))==-1)
         {
            //alert("Invalid E-mail ID")
            return false;
         }

         if (str.indexOf(" ")!=-1)
         {
            //alert("Invalid E-mail ID")
            return false;
         }
         return true		
	 }
	 else
	 {
	    return false;
	 }			
}

function stringMatch(object1, object2)
{
    var objToMatch=document.getElementById(object2);
    var objWithMatch=document.getElementById(object1);
    if(objToMatch && objWithMatch)
    {
        if(objWithMatch.value!=objToMatch.value)
        {            
            return false;
        }
        else
        {
            return true;
        }    
    }
    else
    {
        alert("Objects not found.");
        return false;
    }
}

function validateShipDate(shipDate)
{
    var startdate="";
    var today=new Date();
    if(today.getMonth()<9)
    {
        if(today.getDate()<9)
        {
            startdate="0" + (today.getMonth()+1)+"/"+ "0" + (today.getDate())+"/"+(today.getFullYear());  
        }
        else
        {
            startdate="0" + (today.getMonth()+1)+"/"+today.getDate()+"/"+(today.getFullYear());    
        }
    }
    else
    {
        if(today.getDate()<9)
        {
            startdate=today.getMonth()+1+"/"+ "0" + (today.getDate())+"/"+(today.getFullYear());
        }
        else
        {
            startdate=today.getMonth()+1+"/"+today.getDate()+"/"+(today.getFullYear());    
        }
    }
    if(startdate!=null && shipDate!=null)
     {    
        if (Date.parse(startdate) > Date.parse(shipDate)) 
        {        
            return false;
        }
        else
        {  
            return true;
        }
    }
    else
    {
        return false;
    }
}

function setCity(setto, objDropDown)
{
    var objSetTo=document.getElementById(setto);
    var objOtherCity=document.getElementById("dvOtherCity");
    if(objSetTo!=null && objOtherCity!=null)
    {
        var varSelIndex=objDropDown.selectedIndex;
        objSetTo.value=objDropDown.options[varSelIndex].text;
        if(objDropDown.value=="9999")
        {
            objOtherCity.style.display="block";
        }
        else
        {
            objOtherCity.style.display="none";
        }
    }
    else
    {
        alert("Hidden field not found!");
    }
}


