//JavaScript functions for iMirus Store 6-22-2007

//=============================================================
function onlyForRegisteredUsersMessage()
{
    alert("Please Login or Register before Subscribing to e-mail Lists.");

}

//=============================================================

   function msieversion()
   {
      var ua = window.navigator.userAgent
      var msie = ua.indexOf ( "MSIE " )

      if ( msie > 0 )      // If Internet Explorer, return version number
         return parseInt (ua.substring (msie+5, ua.indexOf (".", msie )))
      else                 // If another browser, return 0
         return 0

   }
   
//=============================================================

    function testSessionCookie() 
    {
        var bSuccess = false;

        try 
        {
            document.cookie = "testSessionCookie=Enabled;";

            if (getCookie("testSessionCookie")=="Enabled")
            {
                bSuccess = true;
            }
            else
            {
                alert("Your session cookies are not enabled. Please enable Session cookies before continuing");
            }
        }
        catch (err) 
        { 
            alert("Your session cookies are not enabled. Please enable Session cookies before continuing");
        }

        return bSuccess;
    }


function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


//=============================================================
function updateBilling(d,uf,whereToGo) {

 
    var error = false;
    var errorMsg = "";
    var regSpaces = /^\s*$/;    

    if ( d.b_name_first.value.match(regSpaces) || d.b_name_last.value.match(regSpaces) ||
	    d.b_addr_1.value.match(regSpaces) || d.b_addr_city.value.match(regSpaces)  ||  d.b_addr_zip.value.match(regSpaces) ||
            d.b_addr_state.selectedIndex == 0  ||  d.b_addr_country.selectedIndex == 0 )
	      
    {
        errorMsg = "Required information is missing from the Billing Information section.\nPlease complete the following fields:\n\n";

        //if (d.b_prefix.selectedIndex == 0)    { errorMsg += "--Prefix\n"; }
        if (d.b_name_first.value.match(regSpaces)) { errorMsg += "--First Name\n"; }
        if (d.b_name_last.value.match(regSpaces)) { errorMsg += "--Last Name\n"; }
        if (d.b_addr_1.value.match(regSpaces)) { errorMsg += "--Address One\n"; }
        if (d.b_addr_city.value.match(regSpaces)) { errorMsg += "--City\n"; }
        if (d.b_addr_zip.value.match(regSpaces)) { errorMsg += "--Zip\n"; }
        if (d.b_addr_state.selectedIndex == 0)    { errorMsg += "--State\n"; }

        if (d.b_addr_country.selectedIndex == 0)    { errorMsg += "--Country\n"; }


        alert(errorMsg);	
        error = true;

    }//close if


    if (d.b_addr_zip.value.length < 5)
    {
        error= true;
        alert("Please check the zip code");
    }


    //if no errors, proceed to CC data
    if (!error) 
    {
        //d.action.value = whereToGo;
        //d.action = whereToGo;
        d.submit();

    }//close proceed to CC data


}//close updateBilling

//================================================================

function chkBilling(d,uf,whereToGo) {

 
    var error = false;
    var errorMsg = "";
    var regSpaces = /^\s*$/;    

    if ( d.b_name_first.value.match(regSpaces) || d.b_name_last.value.match(regSpaces) ||
	    d.b_addr_1.value.match(regSpaces) || d.b_addr_city.value.match(regSpaces) || d.b_addr_zip.value.match(regSpaces) ||
            d.b_addr_state.selectedIndex == 0  ||  d.b_addr_country.selectedIndex == 0  )
	      
    {
        errorMsg = "Required information is missing from the Billing Information section.\nPlease complete the following fields:\n\n";

        
        if (d.b_name_first.value.match(regSpaces)) { errorMsg += "--First Name\n"; }
        if (d.b_name_last.value.match(regSpaces)) { errorMsg += "--Last Name\n"; }
        if (d.b_addr_1.value.match(regSpaces)) { errorMsg += "--Address One\n"; }
        if (d.b_addr_city.value.match(regSpaces)) { errorMsg += "--City\n"; }
        if (d.b_addr_state.selectedIndex == 0)    { errorMsg += "--State\n"; }
        if (d.b_addr_zip.value.match(regSpaces)) { errorMsg += "--Zip\n"; }
        if (d.b_addr_country.selectedIndex == 0)    { errorMsg += "--Country\n"; }




        alert(errorMsg);	
        error = true;

    }//close if


    if (d.b_addr_zip.value.length < 5)
    {
        error= true;
        alert("Please check the zip code");
    }


    //if no errors, proceed to CC data
    if (!error) 
    {
        //d.action.value = whereToGo;
        d.submit();

    }//close proceed to CC data


}//close chkBilling

//========================================================================

function chkForgotForm(d) {

    var error = true;
    var regSpaces = /^\s*$/;


    if (d.uid != undefined && !chkEmail(d.uid)) {
	error = false;
	//d.action = "/iMirusStore/ForgotSecurity";
	d.submit();
    }//close error
    else if (d.answer != undefined && !d.answer.value.match(regSpaces)) {
	error = false;
	//d.action = "/iMirusStore/ForgotSecurity";
	d.submit();
    }//close else if
    else if ((d.passwd != undefined && !d.passwd.value.match(regSpaces) && d.passwd.value.length > 5) && (d.verifypw != undefined && !d.verifypw.value.match(regSpaces))) {
	if (d.passwd.value == d.verifypw.value) {
	    error = false;
	   // d.action = "/iMirusStore/ForgotSecurity";
	    d.submit();
	}//close if
	else {
	    alert("Password fields do not match.\nPlease try again.\n\n");
	    d.verifypw.value = "";
	    error = false;
	}//close else
    }//close else if

    //alert(d.passwd + "\n" + d.passwd.value + "\n" + d.passwd.value.length + "\n" + d.verifypw + "\n" + d.verifypw.value + "\n");

    if (error) {
	alert("Invalid entry for required fields.\nPlease try again.\n\n");
    }//close else

}//close chkForgotForm




//================================================================
//If the country drop down is not other, then set the other field to empty
function clearOtherCountry(countryField, otherField) {
	
	if (countryField.value != "other") {
		otherField.value = "";
	}//close if
	
}//close clearOtherCountry


//================================================================
//Validates country and other country combonations
function chkCountries(country, otherCountry) {
	if ((country.value == "other" && otherCountry.value == "") || (country.value != "other" && otherCountry.value != "")) {
		alert("If other is chosen in the Country Drop down, you must fill out the Other Country field. \n\nIf you have selected a country then the Other Country field must be empty.\n\n");
		return 4;
	}
	else {
		return 0;
	}
}//close chkCountries

//================================================================
//checks passed field for positive or negative number
function chkForNumb(Numb) {
	
	var regNumb = /^[-]?\d+$/;
	if (Numb.value.match(regNumb)){
		return 0;
	}//close if
	else {
		alert("\nThe value entered for this field is not a valid number.\nPlease try again.\n\n");
		Numb.value = "0";
		return 3;
	}//close else

}//close chkForNumb

//================================================================
//Opens error windows with an onLoad or calls the workshop info pages.
function open_win(URL) {
        //alert(URL);
        window.open(URL,"error","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,copyhistory=0,width=550,height=300");
}//close open_win


//================================================================
//Opens error windows with an onLoad or calls the workshop info pages.
function open_sec_popup(URL) 
{
        //alert(URL);
          window.open(URL,"Secure","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,copyhistory=0,width=1100,height=500");

}//close open_sec_popup

//================================================================
//Closes the secured popup window
function close_sec_popup()
{
    window.close();

}//close close_sec_popup

//================================================================

//logs out first, then closes the secured popup window
function logout_close_sec_popup()
{
    window.location('https://www.imirus.com/iMirusStore/Logout');
   // window.close();

}//close logout_close_sec_popup


//================================================================

//Check for valid email address using regular expression.
function chkEmail(strEmail) {
        var regEmail1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
        var regEmail2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
        
        if (strEmail.value.match(regEmail1)){
                alert("Please enter a valid Email address.");
                return true;
        }//close if
        else {
                if (!strEmail.value.match(regEmail2)) {
                        alert("Please enter a valid Email address.");
                        return true;
                }//close if
                else {
                        return false;
                }//close else
        }//close else

}//close chkEmail

//================================================================

//Call Credit Card validation from the store
function storeCreditCheck(whereToGo, value)
{

        var error = false;
        var d = "";

        if (whereToGo == "acct_confirmation_info")
        {
            d = document.acct_confirmation_info;
        }
        else
        {
            d = document.confirmation_info ;
        }

        var checkedVal = "";
        var errorMsg = "";
        var regSpaces = /^\s*$/;  



        //alert("value = " + value );

        if ( value > 0 )
        {
            //alert("ok, past the value test. value = " + value );

            //password check
            if (  d.ccNameOnCard.value.match(regSpaces)  || 
                  d.ccType.value.match(regSpaces)  || d.ccEx1.value.match(regSpaces)  || 
                  d.ccEx2.value.match(regSpaces)  || d.ccNumb.value.match(regSpaces) 
               )
            {
                errorMsg = "Required information is missing from the Billing Information section.\nPlease complete the following fields:\n\n";

                if (d.ccNameOnCard.value.match(regSpaces)) { errorMsg += "--Name on Card\n"; }
                if (d.ccType.value.match(regSpaces)) { errorMsg += "--Card Type\n"; }
                if (d.ccEx1.value.match(regSpaces)) { errorMsg += "--Expiration Month\n"; }
                if (d.ccEx2.value.match(regSpaces)) { errorMsg += "--Expiration Year\n"; }
                if (d.ccNumb.value.match(regSpaces)) { errorMsg += "--Card Number\n"; }

                alert(errorMsg);	
                error = true;
            }//close if  

            if (!error) 
            {
            error = validateCC(d.ccType[d.ccType.selectedIndex].value, d.ccNumb.value, d.ccEx1[d.ccEx1.selectedIndex].value, d.ccEx2[d.ccEx2.selectedIndex].value, d.ccNameOnCard.value);
            }//close error if


        }//close if for value test


        if (!error)
        {
            return false;
        }
        else
        {
            return true;
        }


}//close storeCreditCheck

//================================================================
//Credit Card validation.
function validateCC(ccType, ccNumb, ccEx1, ccEx2, ccNameOnCard) {

        var curDate = new Date();
        var cc_error = 0;
       
        //check for credit card type and if left unchecked alert user.
        if (ccType == "") {
                cc_error = 1;
        }//close if
       
        if (cc_error != 0) {
                alert("Please choose a Credit Card Type.");    
        }//close if
       
        //check for empty in the following fields.
        else if (ccNumb == "" || ccEx1 == "" || ccEx2 == "" || ccNameOnCard == "") {
                cc_error = 2;
                alert("Please enter a Credit Card Number, expiration date and a \"Name Appearing on Card\".");
        }//close else if
       
       
        //First check to see if the ccType contains the correct number of digits and if the digits start with the correct numeral.
        if (cc_error == 0) {
                var ccNumbReg = /^\D+$/;
               
                if (ccNumb.match(ccNumbReg)) {
                        alert("Plese do not enter any special characters or spaces into the credit card number.");
                        cc_error = 2;
                }//close if
                else if (ccType == "Visa" && ((ccNumb.length != 13 && ccNumb.length != 16) || (ccNumb.substr(0,1) != "4"))) {
                        alert("Not a valid Visa number.");
                        cc_error = 2;
                }//close else if
                else if (ccType == "MasterCard" && ((ccNumb.length != 16) || (ccNumb.substr(0,1) != "5"))) {
                        alert("Not a valid MasterCard number.");
                        cc_error = 2;
                }//close else if
                else if (ccType == "American Express" && ((ccNumb.length != 15) || (ccNumb.substr(0,2) != "37"))) {
                        alert("Not a valid American Express number.");
                        cc_error = 2;
                }//close else if
                else if (ccType == "Discover" && ((ccNumb.length != 16) || (ccNumb.substr(0,4) != "6011"))) {
                        alert("Not a valid Discover number.");
                        cc_error = 2;
                }//close else if
                else if (ccType == "Diners Club" && ((ccNumb.length != 14) || (ccNumb.substr(0,2) != "36") && (ccNumb.substr(0,2) != "38"))) {
                        alert("Not a valid Diners Club number.");
                        cc_error = 2;
                }//close else if
               
                //checksum for cc number.
                else {
                       
                        var tempCCNumb = [];
                       
                        //reverse cc numbers.
                        for (var i=(ccNumb.length - 1);i>-1;i--) {
                                tempCCNumb[((ccNumb.length-1)-i)] = ccNumb.substr(i,1);
                        }//close fore
                       
                        var tempDigit = "";
                       
                        /*calculate cc number's validity
                        1. Take the even digits and multiply them by 2
                        2. If the result is greater than 18, substract 9
                        3. Replace old number with newly calculated number
                        4. Add the array values together
                        5. Divide by ten and if the remainder is not = 0 then it's a bad cc number.                    
                        */
                       
                        for (var j=1;j<tempCCNumb.length;j+=2) {
                                tempDigit = tempCCNumb[j];
                                tempDigit *= 2;
                                if (tempDigit > 9) {
                                        tempDigit -= 9;
                                }//close if
                                tempCCNumb[j] = tempDigit;                             
                        }//close for
                       
                        var tempSum = 0;
                        for (var k=0;k<tempCCNumb.length;k++) {
                                tempSum += parseInt(tempCCNumb[k]);
                        }//close for
                       
                        tempSum %= 10;
                       
                        if (tempSum != 0) {
                                cc_error = 2;
                                alert("Invalid Credit Card Number.\nPlease try again.");
                        }//close if
                        //check expiration date.
                        else if (curDate.getFullYear() == parseInt(ccEx2)) {
                                if ((curDate.getMonth()+1) > parseInt(ccEx1)) {
                                        cc_error = 3;
                                        alert("Invalid Expiration Date.\nPlease try again.");
                                }//close if
                        }//close else if
                        else if (curDate.getFullYear() > parseInt(ccEx2)) {
                                cc_error = 3;
                                alert("Invalid Expiration Date.\nPlease try again.");
                        }//close else if
                }//close else
        }//close if
       
       
        if (cc_error != 0) {
                return true;
        }//close if
        else {
                return false;
        }//close else


}//close validateCC



//================================================================
//replace charaters with another charater.
function removeChars(fieldArray, charArray, replaceChar) {
	
	//Loop on first array
	for (var i=0;i<fieldArray.length;i++) {
		var tempField = fieldArray[i].value;
	
		//Loop on second arry
		for (var j=0;j<charArray.length;j++) {

			//Define regular expression
			var regEx = "/[" + charArray[j] + "]+/gi";
			
			//Keep all replaces on current field by asigning the tempField value to the replaced value.
			tempField = tempField.replace(eval(regEx), replaceChar);
		}
		
		//Reasign field value in form
		fieldArray[i].value = tempField;
	}

}//close removeChars


//================================================================	    
function chkLoginForm(whereToGo) 
{

    var error = false;
    var d = document.userLogin;
    var checkedVal = "";
    var errorMsg = "";
    var regSpaces = /^\s*$/;  

//alert("inside chkLoginform and whereToGo = " + whereToGo);



    //email check              
    error = chkEmail(d.uid);            



    if (!error)
    {
	
	   // d.action = "/iMirusStore/Login";
	

	    //password check
	    if ( d.userpassword.value.match(regSpaces))
	    {
		errorMsg = "Required information is missing from the Billing Information section.\nPlease complete the following fields:\n\n";

		if (d.userpassword.value.match(regSpaces)) { errorMsg += "--Password\n"; }

		alert(errorMsg);	
		error = true;

	    }//close if                     

	    if (!error) 
	    {
		d.submit();

	    }//close if                    



    }// close error check if

}//close chkLoginForm

//================================================================	    
function chkLoginUpdate(whereToGo) 
{

    var error = false;
    var d = document.userLoginUpdate;
    var checkedVal = "";
    var errorMsg = "";
    var regSpaces = /^\s*$/;  


    //email check              
    //error = chkEmail(d.uid);            

		errorMsg = "Required information is missing from the Billing Information section.\nPlease complete the following fields:\n\n";

    if (!error)
    {
	



                if (!error)
                {
                    if (d.firstName.value.match(regSpaces)) 
                    {                     
                        alert(errorMsg + "--First Name");	
                        error = true;
                    }
                }

             
                if (!error)
                {
                    if (d.lastName.value.match(regSpaces)) 
                    {                          
                        alert(errorMsg + "--Last Name");	
                        error = true;
                    }
                }


                if (!error)
                {
                    if (d.streetAddress1.value.match(regSpaces)) 
                    {                          
                        alert(errorMsg + "--Street Address");	
                        error = true;
                    }
                }              

                

                if (!error)
                {
                    if (d.city.value.match(regSpaces)) 
                    {                          
                        alert(errorMsg + "--City");	
                        error = true;
                    }
                }

                if (!error)
                {
                    if (d.addr_state.selectedIndex == 0 ) 
                    {                          
                        alert(errorMsg + "--State");	
                        error = true;
                    }
                }

         
                if (!error)
                {
                    if (d.addr_country.selectedIndex == 0 ) 
                    {                          
                        alert(errorMsg + "--Country");	
                        error = true;
                    }
                }

                if (!error)
                {
                    if (d.zipPostalCode.value.match(regSpaces) ) 
                    {                          
                        alert(errorMsg + "--Zip");	
                        error = true;
                    }
                }
                if (!error)
                {
                    if (d.phone.value.match(regSpaces) ) 
                    {                          
                        alert(errorMsg + "--phone");	
                        error = true;
                    }
                }

                if (!error)
                {
                    if (d.my_sec_question.selectedIndex == 0 ) 
                    {                          
                        alert(errorMsg + "--Security Question");	
                        error = true;
                    }
                }

                if (!error)
                {
                    if (d.my_sec_answer.value.match(regSpaces) ) 
                    {                          
                        alert(errorMsg + "--Security Answer");	
                        error = true;
                    }
                }

    }

        if (!error) 
        {
           // d.action = "/iMirusStore/LoginUpdate";
            d.submit();

        }//close if                    


}//close chkLoginUpdate



//====================================================================
function chkRegistrationOnly() 
{

    var error = false;
    var d = document.userRegistration;
    var checkedVal = "";
    var errorMsg = "";
    var regSpaces = /^\s*$/;  


    if (!error)
    {
	
	    //d.action = "/iMirusStore/SimpleNewReg";


	    //password check
	    if (  d.b_name_first.value.match(regSpaces)  || 
            d.b_name_last.value.match(regSpaces)  || d.uid.value.match(regSpaces)  || 
            d.userpassword.value.match(regSpaces)  || d.my_sec_question.value.match(regSpaces)  || d.my_sec_answer.value.match(regSpaces) ||
            d.email_confirm.value.match(regSpaces) || d.password_confirm.value.match(regSpaces)
            )
       
	    {
		errorMsg = "Required information is missing from the Billing Information section.\nPlease complete the following fields:\n\n";

		
                if (d.b_name_first.value.match(regSpaces)) { errorMsg += "--First Name\n"; }
                if (d.b_name_last.value.match(regSpaces)) { errorMsg += "--Last Name\n"; }
                if (d.uid.value.match(regSpaces)) { errorMsg += "--e-Mail Address\n"; }
                if (d.userpassword.value.match(regSpaces)) { errorMsg += "--Password\n"; }
                if (d.my_sec_question.value.match(regSpaces)) { errorMsg += "--Security Question\n"; }
                if (d.my_sec_answer.value.match(regSpaces)) { errorMsg += "--Security Answer\n"; }
                if (d.email_confirm.value.match(regSpaces)) { errorMsg += "--confirm email\n"; }
                if (d.password_confirm.value.match(regSpaces)) { errorMsg += "--confirm password\n"; }

		alert(errorMsg);	
		error = true;
	    }//close if                     



            if (d.password_confirm.value.length < 6)
            {
                error= true;
                alert("Your password must be at least 6 characters long");
            }

            if (d.my_sec_question.value.length < 8)
            {
                error= true;
                alert("Your security question must be at least 8 characters long");
            }

            if (!d.uid.value.match(d.email_confirm.value))
            {
                error= true;
                alert("Your e-mail and the confirm e-mail fields do not match");
            }

            if (!d.userpassword.value.match(d.password_confirm.value))
            {
                error= true;
                alert("Your password and the confirm password fields do not match");
            }

	    if (!error) 
	    {
		d.submit()

	    }//close if                    


	}//close else if

   

}//close chkRegistrationOnly


//================================================================	    
function chkRegistrationForm() 
{

    var error = false;
    var d = document.userRegistration;
    var checkedVal = "";
    var errorMsg = "";
    var regSpaces = /^\s*$/;  


    if (!error)
    {
	
	   // d.action = "/iMirusStore/Login";
	    //alert(checkedVal);

	    //password check
	    if (  d.b_name_first.value.match(regSpaces)  || 
            d.b_name_last.value.match(regSpaces)  || d.uid.value.match(regSpaces)  || 
            d.userpassword.value.match(regSpaces)  || d.my_sec_question.value.match(regSpaces)  || d.my_sec_answer.value.match(regSpaces) ||
            d.email_confirm.value.match(regSpaces) || d.password_confirm.value.match(regSpaces)
            )
       
	    {
		errorMsg = "Required information is missing from the Billing Information section.\nPlease complete the following fields:\n\n";

		
                if (d.b_name_first.value.match(regSpaces)) { errorMsg += "--First Name\n"; }
                if (d.b_name_last.value.match(regSpaces)) { errorMsg += "--Last Name\n"; }
                if (d.uid.value.match(regSpaces)) { errorMsg += "--e-Mail Address\n"; }
                if (d.userpassword.value.match(regSpaces)) { errorMsg += "--Password\n"; }
                if (d.my_sec_question.value.match(regSpaces)) { errorMsg += "--Security Question\n"; }
                if (d.my_sec_answer.value.match(regSpaces)) { errorMsg += "--Security Answer\n"; }
                if (d.email_confirm.value.match(regSpaces)) { errorMsg += "--confirm email\n"; }
                if (d.password_confirm.value.match(regSpaces)) { errorMsg += "--confirm password\n"; }

		alert(errorMsg);	
		error = true;
	    }//close if                     


            if (d.password_confirm.value.length < 6)
            {
                error= true;
                alert("Your password must be at least 6 characters long");
            }

            if (d.my_sec_question.value.length < 8)
            {
                error= true;
                alert("Your security question must be at least 8 characters long");
            }

            if (!d.uid.value.match(d.email_confirm.value))
            {
                error= true;
                alert("Your e-mail and the confirm e-mail fields do not match");
            }

            if (!d.userpassword.value.match(d.password_confirm.value))
            {
                error= true;
                alert("Your password and the confirm password fields do not match");
            }


	    //d.action = "checkout-billing.jsp";
	    if (!error) 
	    {
		d.submit();

	    }//close if


	}//close else if

   

}//close chkRegistrationForm



//===================================================================================
      
    function get_cookie(cookie_name)
    {
    
        var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );


        if ( results )
        {
            return ( unescape ( results[1] ) );
        }
        else
        {
            return null;
        }
    }


//===================================================================================
//===== these 2 functions are to process cookies for use in drop down fields   ======


function createDD_Cookie(name,value,display,days) 
{



	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	//document.cookie = name+"="+value+"|"+display+expires+"; path=/";
        document.cookie = name+"="+value+"|"+display+expires+"; path=/iMirusStore/";
        //document.cookie = name+"="+value+expires+"; path=/iMirusStore/";
        
        checkSameAddr();

}

//===================================================================================

function readDD_Cookie(name) 
{

        var value2split = "";

	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) 
                {    
                    value2split = c.substring(nameEQ.length,c.length);
                    var displayValue = value2split.split('|');
                    return displayValue[0];
                }
	}
	return null;
}


//===================================================================================

function createCookie(name,value,days) {


        /*
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/iMirusStore/";
        */
	document.cookie = name + "=" + value + "; path=/iMirusStore/";

        checkSameAddr();
}

//==============================================================================

function executePurchase(name,value,days,whereToGo)
{   
 

        var error = storeCreditCheck(whereToGo, value);

        //alert("error = " + error);

    
        if (!error)
        {

            var answer = confirm("Are you sure you want to execute this Purchase?")
            if (answer)
            {

                   if (whereToGo == "confirmation_info")
                   {
                    // document.confirmation_info.action="OrderNewReg";
                     document.confirmation_info.submit();
                   }
                   if (whereToGo == "acct_confirmation_info")
                   {
                    // document.acct_confirmation_info.action="OrderExistReg";
                     document.acct_confirmation_info.submit();
                   }


            }
            else
            {
                    alert("Ok, Purchase cancelled. Your products are still in the cart if you want to checkout later.")
            }


        }

}


//==============================================================================

function readCookieName(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) 
        {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) 
                {
                    return c.substring(nameEQ.length,c.length);
                }
	}
	return null;
}


function eraseCookie(name) {
	createCookie(name,"",-1);
}

//===================================================================================
function checkSameAddr() 
{


    if (document.billinginfo)
    {

        if (document.billinginfo.sameAddrAsBilling.checked == true )
        {

            document.billinginfo.s_prefix.value       = document.billinginfo.b_prefix.value ;
            document.billinginfo.s_name_first.value   = document.billinginfo.b_name_first.value ;
            document.billinginfo.s_name_last.value    = document.billinginfo.b_name_last.value ;
            document.billinginfo.s_company.value      = document.billinginfo.b_company.value ;
            document.billinginfo.s_addr_1.value       = document.billinginfo.b_addr_1.value ;
            document.billinginfo.s_addr_2.value       = document.billinginfo.b_addr_2.value ;
            document.billinginfo.s_addr_city.value    = document.billinginfo.b_addr_city.value ;
            document.billinginfo.s_addr_state.value   = document.billinginfo.b_addr_state.value ;
            document.billinginfo.s_addr_zip.value     = document.billinginfo.b_addr_zip.value ;
            document.billinginfo.s_phone.value        = document.billinginfo.b_phone.value ;
            document.billinginfo.s_addr_country.value = document.billinginfo.b_addr_country.value;
         

        }
        else if (document.billinginfo.sameAddrAsBilling.checked == false )
        {

            document.billinginfo.s_prefix.value       = readCookieName('s_prefix');
            document.billinginfo.s_name_first.value   = readCookieName('s_name_first');
            document.billinginfo.s_name_last.value    = readCookieName('s_name_last');
            document.billinginfo.s_company.value      = readCookieName('s_company');
            document.billinginfo.s_addr_1.value       = readCookieName('s_addr_1');
            document.billinginfo.s_addr_2.value       = readCookieName('s_addr_2');
            document.billinginfo.s_addr_city.value    = readCookieName('s_addr_city');
            document.billinginfo.s_addr_state.value   = readCookieName('s_addr_state');
            document.billinginfo.s_addr_zip.value     = readCookieName('s_addr_zip');
            document.billinginfo.s_phone.value        = readCookieName('s_phone');
            document.billinginfo.s_addr_country.value = readCookieName('s_addr_country');
       
             
        }
    }

}

//===================================================================================
function checkSameAddrConfirm() 
{
            document.confirmation_info.s_prefix.value       = document.confirmation_info.b_prefix.value ;
            document.confirmation_info.s_name_first.value   = document.confirmation_info.b_name_first.value ;
            document.confirmation_info.s_name_last.value    = document.confirmation_info.b_name_last.value ;
            document.confirmation_info.s_company.value      = document.confirmation_info.b_company.value ;
            document.confirmation_info.s_addr_1.value       = document.confirmation_info.b_addr_1.value ;
            document.confirmation_info.s_addr_2.value       = document.confirmation_info.b_addr_2.value ;
            document.confirmation_info.s_addr_city.value    = document.confirmation_info.b_addr_city.value ;
            document.confirmation_info.s_addr_state.value   = document.confirmation_info.b_addr_state.value ;
            document.confirmation_info.s_addr_zip.value     = document.confirmation_info.b_addr_zip.value ;
            document.confirmation_info.s_phone.value        = document.confirmation_info.b_phone.value ;
            document.confirmation_info.s_addr_country.value = document.confirmation_info.b_addr_country.value;
          


            createCookie('sameAddrAsBilling','true',100);

            createCookie('b_name_first',document.confirmation_info.b_name_first.value,100);
            createCookie('b_name_last',document.confirmation_info.b_name_last.value,100);    
            createCookie('b_name_company',document.confirmation_info.b_company.value,100);
            createCookie('b_addr_1',document.confirmation_info.b_addr_1.value,100);
            createCookie('b_addr_2',document.confirmation_info.b_addr_2.value,100);
            createCookie('b_addr_city',document.confirmation_info.b_addr_city.value,100);
            createCookie('b_addr_state',document.confirmation_info.b_addr_state.value,100);
            createCookie('b_addr_zip',document.confirmation_info.b_addr_zip.value,100);
            createCookie('b_addr_country',document.confirmation_info.b_addr_country.value,100);
            createCookie('b_phone',document.confirmation_info.b_phone.value,100);
         //   createCookie('uid',document.confirmation_info.uid.value,100);
}
//===================================================================================

//
//
//$$$$$$$$$$$$$$$$$$     GLOBAL FUNCTIONS  for JavaScript   $$$$$$$$$$$$$$$$$$$$$$$$$
//
// 
        
//===================================================================================            
            
    function mouse_over( objname, img )
    {
        document.images[objname].src = 'assets/images/' + img + '_active.gif';
    }
            
//===================================================================================            
            
    function mouse_out( objname, img )
    {
        document.images[objname].src = 'assets/images/' + img + '_normal.gif';
    }

//===================================================================================            

    function check_out()
    {
    //alert ('Ok, ready to check out now');
    document.billinginfo.submit();
    }            

//===================================================================================            
            
    function setCooke(name)
    {
        var cooke = '';
    cooke = get_cookie(name);
    return cooke.getvalue();
    
    }

//==================================================================================
    function emptyCart(formName)
    {



    if ( confirm ( 'Are you sure you want to remove everything from the Shopping Cart ?'))
    {

        var newProductsCookie = "products = *";

        document.cookie =  newProductsCookie ;   



    }
        
       
        
           if (formName == 'cartformreload')
           {
             document.cartformreload.submit();
           }
           
           if (formName == 'confirmation_info')
           {
             document.confirmation_info.submit();
           }
                      
           if (formName == 'acct_confirmation_info')
           {
             document.acct_confirmation_info.submit();
           }
                          
        

     return "0";

    }



//===================================================================================
   function removeCartItem(prodID,couponID,formName)
    {



    if ( confirm ( 'Are you sure?'))
    {

        var cooke = '';
        cooke = get_cookie('products');

        // now that we have the cookie and the product id to remove, we are going to spin through
        // the cookie and build it again without the product id that is being removed.


        var ca = cooke.split('*');
        var newProductsCookie = "products = *";

            for(var i=1; i < ca.length - 1; i++) 
            {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces

                if ( prodID == ca[i] )
                {
                    //alert("Found the prod : " + prodID);
                }
                else if ( couponID == ca[i]) {
                    //alert("Found the coupon : " + couponID)
                }
                else
                {
                  newProductsCookie = newProductsCookie + ca[i] + '*';
                }
            }



    }
        
           document.cookie =  newProductsCookie ;      
        
           if (formName == 'cartformreload')
           {
             document.cartformreload.submit();
           }
           
           if (formName == 'confirmation_info')
           {
             document.confirmation_info.submit();
           }
                      
           if (formName == 'acct_confirmation_info')
           {
             document.acct_confirmation_info.submit();
           }
                          
        

     return "0";

    }

//===================================================================================            
            
    function updateCookieUp(prodID,formName,quantity)
    {

    var cooke = '';
    cooke = get_cookie('products');

    // now that we have the cookie and the product id to remove, we are going to spin through
    // the cookie and build it again without the product id that is being removed.


    startingQuantity = parseInt(quantity);
    startingQuantity += 1;
    
    var ca = cooke.split('*');
    var newProductsCookie = "products = *";

        for(var i=1; i < ca.length - 1; i++) 
        {
            var c = ca[i];
        
            var q = c.split('-');
        
            justProdId = q[0];
            justQnty   = q[1];
        
            while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces

            if ( prodID == justProdId)
            {
             
              newProductsCookie = newProductsCookie + justProdId + '-' + startingQuantity + '*';
            }
            else
            {
              newProductsCookie = newProductsCookie + ca[i] + '*';
            }
        }


           document.cookie =  newProductsCookie ;      
        
           
           if (formName == 'cartformreload')
           {
             document.cartformreload.submit();
           }
           
           if (formName == 'confirmation_info')
           {
             document.confirmation_info.submit();
           }

           if (formName == 'acct_confirmation_info')
           {
             document.acct_confirmation_info.submit();
           }
                      
           
        

     return "0";

    }

//===================================================================================            
            
    function updateCookieDown(prodID,formName,quantity)
    {

    var cooke = '';
    cooke = get_cookie('products');

    // now that we have the cookie and the product id to remove, we are going to spin through
    // the cookie and build it again without the product id that is being removed.


    startingQuantity = parseInt(quantity);
    if (startingQuantity > 1)
    {
        startingQuantity -= 1;
    }
    
    var ca = cooke.split('*');
    var newProductsCookie = "products = *";

        for(var i=1; i < ca.length - 1; i++) 
        {
            var c = ca[i];
        
            var q = c.split('-');
        
            justProdId = q[0];
            justQnty   = q[1];
        
            while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces

            if ( prodID == justProdId)
            {
             
              newProductsCookie = newProductsCookie + justProdId + '-' + startingQuantity + '*';
            }
            else
            {
              newProductsCookie = newProductsCookie + ca[i] + '*';
            }
        }


           document.cookie =  newProductsCookie ;      
        
           
           if (formName == 'cartformreload')
           {
             document.cartformreload.submit();
           }
           
           if (formName == 'confirmation_info')
           {
             document.confirmation_info.submit();
           }
                      
            if (formName == 'acct_confirmation_info')
           {
             document.acct_confirmation_info.submit();
           }          
        

     return "0";

    }

                        
                  
//===================================================================================             
    function mouse_over( objname, img )
    {
        document.images[objname].src = 'assets/images/' + img + '_active.gif';
    }

    function mouse_out( objname, img )
    {
        document.images[objname].src = 'assets/images/' + img + '_normal.gif';
    }

//===================================================================================           
            
            
    function setMyCookie(value)
    {
    
        document.cookie = "displayChoice = " + value;
        document.displayChoice.submit();   
    }

        
//===================================================================================       
        
        
    function startShoppingCart(prodID,cookieName,quantity)
    {    
       
    
      cooke = "";
      cooke = get_cookie(cookieName);
   


      if (cooke == null )
      { 
        //document.cartform.action="buildProductsCookie.jsp";
        document.cookieDeleted.submit();
      //  alert('Shopping cart is now setup. Try it again.');
      //  history.back();
      }
  else{

           if ( readCookie(prodID) == '0' ) 
           {
               if (cooke == 'null')
               {                
                   document.cookie = cookieName + ' = ' + prodID + '-'+quantity+'*';
               }
               else
               {
                   document.cookie = cookieName + ' = ' + cooke + prodID  +'-'+quantity+'*';
               }
               
               document.cartform.submit();         
           }
           else
           {
               document.cartform.submit();
              //alert ('You have already added that product to your shopping cart.'); 
           }

 }
           
    }   

//===================================================================================            
           
    function readCookie(prodID)
    {
    var ca = document.cookie.split('*');
    var nameEQ = "products" + "=";

        for(var i=0; i < ca.length; i++) 
        {
        var c = ca[i];
        
        var q = c.split('-');

        justProdId = q[0];
        justQnty   = q[1];
        
        
        while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces

            if ( prodID == justProdId)
            {
              //alert("Dup");
              return "1";
            }
        }
    return "0";
    }   




//===================================================================================
           
        
    function catagoryChanged()
    {
        document.displayChoice.submit() ;
    }

//===================================================================================

    function openform(formName)
    {
    
        if (formName == 'payment_info')
        {
            document.payment_info.submit();
        }
        
        if (formName == 'cartform')
        {
            document.cartform.submit();
        }    
    }
    

    
//===================================================================================    
    function drmDesc()
    {
        alert ("Digital Rights Management, DRM, is a form of encryption that prevents users from sharing copyright material. If the lock symbol appears to the right of each product, then the publisher or copy right owner has enabled DRM for this particular file. After download, this file can only be viewed inside your iMirus digital reader. If the lock symbol does not appear, then the publisher or copy right owner has disabled DRM, and this file can be shared with other users.");

    }

//===================================================================================  

//$$$$$$$$$$$$$$$$$$$$$  end  GLOBAL FUNCTIONS jsp   $$$$$$$$$$$$$$$$$$$$$$$$$$$$$




