﻿// JScript File
// Designed for /shop/albums/default.aspx and /shop/accessor/default.aspx pages

/**
 * album pages Functions
 */

function closeenlgdiv() {    
	hidediv("enlgdiv");
	hidediv("pageDimmer");	
}

function showenlgdiv() { 
    showdiv("pageDimmer");		
	showdiv("enlgdiv");
}

function DisplayEnlgImg(img){
var _docHeight = (document.height !== undefined) ? document.height : document.body.offsetHeight;
    	getId("enlgimg").src = "/images/share/loading.gif";
    	showenlgdiv();
    	getId("enlgimg").src = img.src.replace("m_","l_"); 
    	if (_docHeight  < 680) 
        	getId("pageDimmer").style.height = "710px";
}

function switchPreview(imgsrc){
         getId("mainpic").src = "/images/share/loading.gif";
            
        if (imgsrc == "giftbox") //handle giftbox preview
            getId("mainpic").src = "/images/share/m_giftbox.jpg";
        else        
            getId("mainpic").src = imgsrc.src.replace(/t_/i,"m_");  
            
        getId("mainpic").alt = "click to enlarge";
        getId("mainpic").title = "click to enlarge";
}
 
 
var skus = new Array();  

// Holds all the SKU attrs
 function sku(prod, sk, price, saleprice) {
  this.prod = prod;
  this.sk = sk;
  this.price = price;
  this.saleprice = saleprice;
}

//adds a sku to the "skus" array
function addsku(prod, sk, price, saleprice){
          skus.push(new sku(prod, sk, price, saleprice));
}

//loads all the cards from the "cards" array to the "gcList" drop down
function loadCards(){
 var dropdownlist = getId("gcList");
  for (var j = 0; j < skus.length; j++) {  
      addOption(dropdownlist, skus[j].prod + ' - $' + skus[j].price, skus[j].sk); 
      }       
}

// handles "ProductList" dropdown onChange event (Default.aspx)
function updateselection()
{    

    if (getId("gcList").selectedIndex != 0) 
    {
        showdiv("EmailForm");
        showdiv("cartbutton"); 
        showdiv("resetbutton");   
        getId("footer").style.top = "625px";    
        review();
    }

    else
    {
        hidediv("EmailForm");
        hidediv("cartbutton"); 
        hidediv("resetbutton"); 
        hidediv("review"); 
        getId("footer").style.top = "525px"; 
    }    
    
}

function review()
{
     //populate review data
    var reviewdata= "<font class='prodSubTitle'>YOUR SELECTION:</font> " +
    "<img src='/images/share/spacebox.gif' height='20' align='top'/><br />";
  
     for (var j = 0; j < skus.length; j++) {
        if (skus[j].sk  == getId("gcList").value){
            reviewdata=reviewdata+skus[j].prod+"</b>";
            reviewdata=reviewdata+":<b>$ "+skus[j].price+" ea.</b>";
            reviewdata=reviewdata+"<br />#"+skus[j].sk; 
            showdiv("cartbutton"); 
            hidediv("cartbuttonoff"); 
            showdiv("cartbuttonoff"); 
            break;        
        }
    }
    reviewdata=reviewdata+"<br />Quantity: 1";
    reviewdata=reviewdata+"<br />Availability: In Stock";
    // if no color is selected yet, hide "add to cart" button div
    if (getId("gcList").selectedIndex == 0){
        hidediv("cartbutton"); 
        showdiv("cartbuttonoff"); 
        showdiv("cartbuttonoff"); 
    }
    // otherwise, if color selected, update form for miva 
    //and show "add to cart" button div
    else {
        updateForm();
        showdiv("cartbutton");  
        hidediv("cartbuttonoff");      
    }
    //write and display review data
    getId("reviewdata").innerHTML = reviewdata;
    showdiv("review");  
}



function checkEmail()
{

        theEmailAdd = getId("email").value;
        theName = getId("name").value;
        emailAddLen = theEmailAdd.length;
        MySplitEmailAdd = theEmailAdd.split("@");
        MySplitEmailAdd2 = theEmailAdd.split(".");
        locAT =theEmailAdd.indexOf("@")  
        getId("email").focus();      

        if ((theName.length <= 0) ||(theName.length == null))
		        {
		            alert("Please enter a valid recipient name");
		            getId("name").focus(); 
		            return false;
		        }
        else if((MySplitEmailAdd[0] == null) || (MySplitEmailAdd[0] == "") ||
		        (MySplitEmailAdd[1] == null) || (MySplitEmailAdd[1] == ""))
		        {
		            alert("Please enter a valid email address");		            
			        return false;
		        }		
		else if((MySplitEmailAdd2[0] == null) || (MySplitEmailAdd2[0] == "") ||
		        (MySplitEmailAdd2[1] == null) || (MySplitEmailAdd2[1] == ""))
		        {
		            alert("Please enter a valid email address");
			        return false;
		        }
		else if ((theEmailAdd.charAt(emailAddLen-1) == "@") 
		        || (theEmailAdd.charAt(emailAddLen-1) == ".")
		        || (theEmailAdd.substring(locAT-1,locAT)==".")
		        || (theEmailAdd.substring(locAT+1,locAT+2)=="."))
		        {
		            alert("Please enter a valid email address");
			        return false;
		        }
		else if (theEmailAdd.indexOf(" ")!=-1)
		        {
		            alert("Please enter a valid email address");
		            return false;
		        }
		else
		    return true;
		    
}

function resetForm()
{
    getId("gcList").selectedIndex = 0;
    updateselection();
}

function updateForm()
{
    document.myform.action = "http://kolo.com/mm5/merchant.mvc?&addpid="+getId("gcList").value;
    getId("Product_Code").value = getId("gcList").value; //"cert50";//

}



/**
Common Product Functions
**/

//Checks stock for the given sku and qty using ajax
function checkStock(sku, qty){  
     ajxResponseDiv = document.getElementById('stockstatus');   
     if(sku != ""){       
            var requestURL = '/shop/albums/stockstatus.asp';            
            var url = requestURL + "?sku=" + sku + "&qty=" + qty;
		    var d = new Date();  		    
            url = url + "&t=" + d.getTime(); //Uncache URL    
                 
			ajxResponseDiv.innerHTML = "Availability: checking stock..."

	        //Create the xmlHttp object to use in the request
            //stateChangeHandler will fire when the state has changed, i.e. data is received back
            // This is non-blocking (asynchronous)
            xmlHttp = GetXmlHttpObject(stateChangeHandler);
            
            //Send the xmlHttp get to the specified url
            xmlHttp_Get(xmlHttp, url);
       }
      else
       ajxResponseDiv.innerHTML = ""
			
}


//Ajax global Vars
var xmlHttp;
var ajxResponseDiv;
/**
Ajax functions
**/

    var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0;
    var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5")!=-1) ? 1 : 0;
    var is_opera = ((navigator.userAgent.indexOf("Opera6")!=-1)||(navigator.userAgent.indexOf("Opera/6")!=-1)) ? 1 : 0;
    //netscape, safari, mozilla behave the same???
    var is_netscape = (navigator.userAgent.indexOf('Netscape') >= 0) ? 1 : 0;
    
    //stateChangeHandler will fire when the state has changed, i.e. data is received back
    // This is non-blocking (asynchronous)
    function stateChangeHandler()
    {	//readyState of 4 or 'complete' represents that data has been returned
        if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete'){
            //Gather the results from the callback
            var str = xmlHttp.responseText;
            //Populate the innerHTML of the div with the results
            ajxResponseDiv.innerHTML = str;
            
            //call state change functions
            handleAjxStatChg();
        }
    }

    // XMLHttp send GET request
    function xmlHttp_Get(xmlhttp, url) {
        xmlhttp.open('GET', url, true);
        xmlhttp.send(null);
    }

    function GetXmlHttpObject(handler) {
        var objXmlHttp = null;    //Holds the local xmlHTTP object instance

        //Depending on the browser, try to create the xmlHttp object
        if (is_ie){
            //The object to create depends on version of IE
            //If it isn't ie5, then default to the Msxml2.XMLHTTP object
            var strObjName = (is_ie5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP';
            
            //Attempt to create the object
            try{
                objXmlHttp = new ActiveXObject(strObjName);
                objXmlHttp.onreadystatechange = handler;
            }
            catch(e){
            //Object creation errored
                alert('IE detected, but object could not be created. Verify that active scripting and activeX controls are enabled');
                return;
            }
        }
        else if (is_opera){
            //Opera has some issues with xmlHttp object functionality
            alert('Opera detected. The page may not behave as expected. Use Firefox or Internet Explorer');
            return;
        }
        else{
            // Mozilla | Netscape | Safari
            objXmlHttp = new XMLHttpRequest();
            objXmlHttp.onload = handler;
            objXmlHttp.onerror = handler;
        }
        
        //Return the instantiated object
        return objXmlHttp;
    }

/**
help functions
**/

//string1.split("(")[0].replace(/\s+$/,"") <-- Removes everthing after "("

function addOption(selectbox,text,value)
{
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    selectbox.options[selectbox.length] = optn;
    //selectbox.options.add(optn); //does not work with firefox

}

function checknumber(testData){
   var anum=/(^\d+$)|(^\d+\.\d+$)/
    if (anum.test(testData))
           return true;
    else
           return false;
}
