﻿
    
/*Fade & Rotate Images*/
var $;
if (!$ && document.getElementById) {
    $ = function() {
        var elements = new Array();
        for (var i = 0; i < arguments.length; i++) {
            var element = arguments[i];
            if (typeof element == 'string') {
            element = document.getElementById(element);
        }
            if (arguments.length == 1) {
                return element;
            }
        elements.push(element);
        }
    return elements;
    }
} else if (!$ && document.all) {
    $ = function() {
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];
        if (typeof element == 'string') {
        element = document.all[element];
    }
    if (arguments.length == 1) {
        return element;
    }
        elements.push(element);
    }
        return elements;
    }
}


var fadeimages=new Array();
var changeimages = new Array();
var fadebgcolor="white";

var fadearray=new Array();
var fadeclear=new Array();

var dom=(document.getElementById) //modern dom browsers
var iebrowser=document.all;

var imagewidth = null;
var imageheight = null;


function fadeshow(theimages, fadewidth, fadeheight, borderwidth, delay, pause, displayorder) {
    imagewidth =  fadewidth;
    imageheight = fadewidth;
    this.pausecheck = pause;
    this.mouseovercheck = 0;
    this.delay = delay;
    this.degree = 10 ;//initial opacity degree (10%)
    this.curimageindex = 0;
    this.imagewidth = fadewidth;
    this.imageheight = imageheight;
	this.nextimageindex = 1;
    fadearray[fadearray.length] = this;

    this.slideshowid = fadearray.length - 1;

    this.canvasbase = "canvas" + this.slideshowid;
    this.curcanvas = this.canvasbase + "_0";
    if (typeof displayorder != "undefined")
        theimages.sort(function() {
            return 0.5 - Math.random();
        }) 
    this.theimages = theimages;
    this.imageborder = parseInt(borderwidth);
    this.postimages = new Array(); //preload images
    for (p = 0; p < theimages.length; p++) {
        this.postimages[p] = new Image();
        this.postimages[p].src = theimages[p][0];
    }

    var fadewidth = fadewidth; //+ this.imageborder * 2;
    var fadeheight = fadeheight; //+ this.imageborder * 2;
    
    if (iebrowser && dom || dom) { //if IE5+ or modern browsers (ie: Firefox)
        document.write('<div class="productImage" id="master' + this.slideshowid + '" style="width:' + fadewidth + 'px;height:' + fadeheight + 'px;overflow:hidden;z-index:0"><div id="' + this.canvasbase + '_0" style="position:absolute;width:' + fadewidth + 'px;height:' + fadeheight + 'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.alpha(opacity=10);-moz-opacity:10;-khtml-opacity:10;background-color:' + fadebgcolor + '"></div><div id="' + this.canvasbase + '_1" style="position:absolute;width:' + fadewidth + 'px;height:' + fadeheight + 'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.alpha(opacity=10);-moz-opacity:10;background-color:' + fadebgcolor + '"></div></div>');
    } else {
        document.write('<div><img name="defaultslide' + this.slideshowid + '" src="' + this.postimages[0].src + '"></div>');
    }
    
    
    if (iebrowser && dom || dom) //if IE5+ or modern browsers such as Firefox
        this.startit()
    else {
		this.curimageindex++;
        setInterval("fadearray[" + this.slideshowid + "].rotateimage(timeToRotate,0)", this.delay);

    }
}

function fadepic(obj) {
    if (obj.degree < 100) {
        obj.degree += 10;
        if (obj.tempobj.filters && obj.tempobj.filters[0]) {
            if (typeof obj.tempobj.filters[0].opacity == "number") //if IE6+
                obj.tempobj.filters[0].opacity = obj.degree;
            else //else if IE5.5-
                obj.tempobj.style.filter = "alpha(opacity=" + obj.degree + ")";
        }
        else if (obj.tempobj.style.MozOpacity)
            obj.tempobj.style.MozOpacity = obj.degree / 101
        else if (obj.tempobj.style.KhtmlOpacity)
            obj.tempobj.style.KhtmlOpacity = obj.degree / 100
    }
    else {

        clearInterval(fadeclear[obj.slideshowid]);
        obj.nextcanvas = (obj.curcanvas == obj.canvasbase + "_0")? obj.canvasbase + "_0" : obj.canvasbase + "_1";
        obj.tempobj = iebrowser? iebrowser[obj.nextcanvas] : document.getElementById(obj.nextcanvas)
        obj.populateslide(obj.tempobj, obj.nextimageindex);
        currentId = obj.nextimageindex;
        obj.nextimageindex = (obj.nextimageindex < obj.postimages.length - 1)? obj.nextimageindex + 1 : 0;

        obj.timeout = setTimeout("fadearray[" + obj.slideshowid + "].rotateimage(timeToRotate, "+ currentId +")", obj.delay);
    }
}

var attribute = new Object()
attribute.freeShipping = false;
attribute.freePersonalization = false;
attribute.personalized = false;

fadeshow.prototype.populateslide = function(picobj, picindex) {
    var slideHTML = "";
    slideHTML += '<table cellpadding="0" cellspacing="0"><tr><td style="vertical-align: middle; text-align: center; width: ' + imagewidth + 'px; height: ' + imageheight + 'px;">';

    if (this.theimages[picindex][1] != "") //if associated link exists for image
        slideHTML = '<a href="' + this.theimages[picindex][1] + '" title="CLICK TO VIEW LARGER IMAGE">';

    slideHTML += '<img src="' + this.postimages[picindex].src + '" id="img' + picindex + '" style="border:solid ' + this.imageborder + 'px #d6dac3;">';
    if (attribute.personalized == true) { slideHTML += '<img class="largeproductimage" src="http://site.photoframesplus.com/images/personalized-300x300.gif" />'; }
    if (attribute.freePersonalization == true) { slideHTML += '<img class="largeproductimage" src="http://site.photoframesplus.com/images/free-personalization-300x300.gif" />'; }
    if (attribute.freeShipping == true) { slideHTML += '<img class="largeproductimage" src="http://site.photoframesplus.com/images/free-shipping-300x300.gif" />'; }
    if (this.theimages[picindex][1] != "") //if associated link exists for image
        slideHTML += '</a>';


    slideHTML += '</td></tr></table>';


    slideHTML += "";
    picobj.innerHTML = slideHTML;
}

fadeshow.prototype.restartit = function (index) {
    this.stop = 0;
    this.curimageindex = index;
    this.startit();
}

fadeshow.prototype.stopit = function () {
    this.stop = 1;

    clearTimeout(this.timeout);
    clearTimeout(this.rotateTimer);
    clearInterval(fadeclear[this.slideshowid]);
}

fadeshow.prototype.rotateimage = function(callback, index) {
    if (this.pausecheck == 1) //if pause onMouseover enabled, cache object
        var cacheobj = this;

    if (this.stop == 1)
        return;
    else if (this.mouseovercheck == 1)
        this.rotateTimer = setTimeout(function() { cacheobj.rotateimage(callback, index) }, 100);
    else
        this.rotate(callback, index);

    this.curimageindex = (this.curimageindex < this.postimages.length - 1)? this.curimageindex + 1 : 0;
}

fadeshow.prototype.rotate = function (callback, index) {
    this.curimageindex = index;
    if (iebrowser && dom || dom) {
        this.resetit();
        var crossobj = this.tempobj = iebrowser? iebrowser[this.curcanvas] : document.getElementById(this.curcanvas);
        crossobj.style.zIndex++;
        fadeclear[this.slideshowid] = setInterval("fadepic(fadearray[" + this.slideshowid + "])", 10);
        this.curcanvas = (this.curcanvas == this.canvasbase + "_0")? this.canvasbase + "_1" : this.canvasbase + "_0";
        callback(index);
    }
    else {
        var ns4imgobj = document.images['defaultslide' + this.slideshowid];
        ns4imgobj.src = this.postimages[this.curimageindex].src
        callback(index);
    }
}

fadeshow.prototype.resetit = function() {
    this.degree = 10;
    var crossobj = iebrowser? iebrowser[this.curcanvas] : document.getElementById(this.curcanvas);
    if (crossobj.filters && crossobj.filters[0]) {
        if (typeof crossobj.filters[0].opacity == "number") //if IE6+
            crossobj.filters(0).opacity = this.degree;
        else //else if IE5.5-
            crossobj.style.filter = "alpha(opacity=" + this.degree + ")";
    }
    else if (crossobj.style.MozOpacity)
        crossobj.style.MozOpacity = this.degree / 101;
    else if (crossobj.style.KhtmlOpacity)
        crossobj.style.KhtmlOpacity = obj.degree / 100;
}


fadeshow.prototype.startit = function() {
    var crossobj = iebrowser? iebrowser[this.curcanvas] : document.getElementById(this.curcanvas);
    this.populateslide(crossobj, this.curimageindex);
    if (this.pausecheck == 1) { //IF SLIDESHOW SHOULD PAUSE ONMOUSEOVER
        var cacheobj = this;
        var crossobjcontainer = iebrowser? iebrowser["master" + this.slideshowid] : document.getElementById("master" + this.slideshowid);
        crossobjcontainer.onmouseover = function() {
            cacheobj.mouseovercheck = 1;
        }
        crossobjcontainer.onmouseout = function() {
            cacheobj.mouseovercheck = 0;
        }
    }
    this.rotateimage(timeToRotate, this.curimageindex);
}


function timeToRotate(index) {
   for (i = 0; i < changeimages.length; i++) {
         var name = "menu" + i;
         if (index == i) {
             if ($(name)) {
                if (typeof changeimages[i][1]!="undefined")
                    $(name).src = changeimages[i][0];
                
                $(name).style.border="solid 1px #cc6600";
            }
         }
         else {
             if ($(name)) {
                if (typeof changeimages[i][1]!="undefined")
                    $(name).src = changeimages[i][1];
                    
                $(name).style.border="solid 1px #d6dac3";
            }
         }
     }
 }
 
function overImageRoll(index) {
    imageRotator.stopit();
    imageRotator.restartit(index);
}

var dropdown = new objDropdown();
var product = new objProduct();
var options = new Collection();

function option() {
    this.showList = null;
    this.quantity = null;
}

product.minumumQuantity = null;

//options.sources = new Collection();
options.items = new Collection();
options.source = null;
options.trigger = null;
options.showList = null;
options.quantity = null;
options.addToCart = null;

options.AddItem = function(source) {
    var items
    var item = new option()
    
    item.source = source;
    item.showList = options.showList.toLowerCase(); 
    item.quantity = options.quantity;
    item.addToCart = options.addToCart;
    
    source = source.toLowerCase();

    if(options.Exists(source)) {
        items = options.Item(source);
    } else {
        items = new Collection();
    }
   
    items.Add(options.trigger,item)
    options.Add(source,items);

    options.showList = null;
    options.quantity = null;
    options.addToCart = null;
}


options.showLink = function(id,text,link,h,w) {
    var baseurl = "http://site.photoframesplus.com/" 
    container = document.getElementById("container-" + id);
    if(container!=null) {
        container.innerHTML = "<img style='border: 0;' src='" + baseurl + "images/tvicon.gif' /><a style='color: #cc6600; position: relative; top: -1px; margin-left: 3px; font-size: 11px;' href=\"javascript:common.openWindow('" + baseurl + link + "','" + h + "','" + w + "')\">" + text + "</a>"; 
    }
}

function objDropdown() {
    this.onchange = _onchange;
    
    function _onchange(dropdown) {
        var index  = dropdown.selectedIndex;
        var source = dropdown.name.toLowerCase();
        var trigger = product.scrubId(dropdown.options[index].value);    
        var item

        if (options.Exists(source)) {
            item = options.Item(source);
            
            product.hideOptions(item);
            product.minumumQuantity = null;
                
            if(item.Exists(trigger)==true) {
                var option = item.Item(trigger)
                
                product.verifyQuantity(option.quantity); 
                product.showOptions(option.showList);
            }
        } 
//        product.finalize(source,option);
        changePrice(dropdown.form);
    }
}

function objProduct() {
    this.verifyQuantity = _verifyQuantity;
    this.validateOptions = _validateOptions;
    this.showOptions = _showOptions;
    this.hideAllOptions = _hideAllOptions;
    this.hideOptions = _hideOptions;
    this.showHideOptions = _showHideOptions;
    this.scrubId = _scrubId;
//    this.finalize = _finalize;
    
//    var finalize_source = null;
//    function _finalize(source,option) {
//        var placeholder = document.getElementById("itemPlaceHolder");
//        
//        if(source==finalize_source) {
//            placeholder.innerHTML = "";
//            finalize_source = null;
//        }
//        
//        if(placeholder != null){
//            if(typeof option != "undefined") { 
//                if(option.addToCart != null) {
//                    placeholder.innerHTML = "<input name='multiple-add' type='hidden' value='1'><input name='vwquantity0' id='vwquantity0' type='hidden'><input name='vwitem0' id='vwitem0' type='hidden' value=''><input name='vwquantity1' type='hidden' value='1'><input name='vwitem1' type='hidden' value='"+option.addToCart+"'>";
//                    finalize_source = source;
//                }
//            }
//        }
//    }
    
    function _verifyQuantity(quantity) {
        var element = document.getElementById("vwquantity")
        
        if (quantity != null) {
            if (element.value < quantity) {
                alert("You must purchase the minimum quantity of " + quantity + " needed for this selection. \n\n Your quantity has been updated automatically to the minimum needed!");
                element.value = quantity;
            }
            
            product.minumumQuantity = quantity;
        } 
    }								
	
    function _validateOptions() {
        var isValid = true;
        var count = document.orderform.elements.length;
        
        for (i=0; i<count; i++) {
            var element = document.orderform.elements[i]; 

	        if ((element.type=="select-one" || element.type=="text") && element.disabled == ""){
	            if ((element.value == "") || (element.value == "Please Select")) {
			        isValid = false;
			        alert ("Please select a " + element.id.toProperCase() + ".");
			        break;
			    } 
	        }
	    }
    	
//    	var placeholder = document.getElementById("itemPlaceHolder");
//    	
//    	if(placeholder != null){
//    	    if(placeholder.innerHTML.length != 0){
//    	        document.getElementById("vwquantity0").value=document.getElementById("vwquantity").value;
//    	        document.getElementById("vwitem0").value=document.getElementById('vwitem').value; 
//    	        
//    	        alert(document.getElementById("vwitem0").value);
//    	        alert(document.getElementById("vwquantity0").value);
//    	    }
//    	}
    	
	    return isValid;
    }

    function _showOptions(ids) {
        if (ids != null) {
            ids =ids.split(",")
            
            for (x=0; x<ids.length; ++x) {
                product.showHideOptions(ids[x],true);
            }
        }
    }
    
    function _changeBackgroundColor(items, count) {
        var color = "#e5ecc9"
        
        for(var i = 0; i < items.Count(); i++) {
            var ids = items.Item(i).showList;

            if (ids != null) {
                ids =ids.split(",")

                for (x=0; x<ids.length; ++x) {
                    document.getElementById("tr-"+ids[x]).style.backgroundColor = color;
                    document.getElementById("tr-"+ids[x]).style.textIndent = "10px";
                }
            }
        }
    }
    
    function _hideAllOptions() {
        for(var i = 0; i < options.Count(); i++) {
            product.hideOptions(options.Item(i));
            _changeBackgroundColor(options.Item(i),i);
        }
    }

    function _hideOptions(items) {
        for(var i = 0; i < items.Count(); i++) {
            var ids = items.Item(i).showList;

            if (ids != null) {
                ids =ids.split(",")

                for (x=0; x<ids.length; ++x) {
                    product.showHideOptions(ids[x],false);
                }
            }
        }
    }

    function _showHideOptions(id,show) {
       document.getElementById("tr-"+id).style.display = common.iif(show,"","none");

        switch (id) {
            case "monogram":
                document.getElementById("monogram-1").disabled=common.iif(show,"","disabled");
                document.getElementById("monogram-2").disabled=common.iif(show,"","disabled");
                document.getElementById("monogram-3").disabled=common.iif(show,"","disabled");
                break;
                
            default:
                if(document.getElementById(id)!=null) {
                    if(document.getElementById(id).selectedIndex!="undefined" && show==false) {
                        document.getElementById(id).selectedIndex=0;
                        document.getElementById(id).disabled=common.iif(show,"","true");
                    }   else {
                        document.getElementById(id).disabled=common.iif(show,"","disabled");
                    }                    
                    
                } else {
                    _findControl("tr-"+id,show);
                }
        }
    }
    
    function _findControl(parentId,show) {
        var pe
        
        if(document.getElementById(parentId).childNodes.length==2) {
            pe = document.getElementById(parentId).childNodes[1];
        } else {
            pe = document.getElementById(parentId).childNodes[3];
        }
        
        for( var x = 0; x < pe.childNodes.length; x++ ) {
            var name = pe.childNodes[x].tagName;
            
            if(typeof(name)!="undefined") {
                if(name.toLowerCase()=="input") {
                    pe.childNodes[x].disabled=common.iif(show,"","disabled")
                }
            }
        }
    }
    
    function _scrubId(id) {
        if(typeof(id) != 'undefined') {
            var s = id.indexOf("(");
            
            if(s >= 0) {
                id = id.substring(0,s);
            }
            
            return common.trim(id);
        }
    }
}

//Changes to 2 decimal
function formatAsMoney(amount) {
    amount -= 0;
    amount = (Math.round(amount*100))/100;
    return (amount == Math.floor(amount)) ? amount + '.00' 
              : ( (amount*10 == Math.floor(amount*10)) ? 
                       amount + '0' : amount);
}
//Used to change price
function changePrice(thisform){
    if((document.getElementById("baseprice")==null) || (document.getElementById("price")==null))
        return;
        
	var checkpricetype;
	var price = document.getElementById("baseprice").value;
	var modPrice = "";
	if(price != "")
		price = parseFloat(price);
	else
		price = 0;

	for(i=0;i<thisform.elements.length;i++){
		if(thisform.elements[i].type == "select-one" && thisform.elements[i].selectedIndex >= 0){
			priceval = thisform.elements[i].options[thisform.elements[i].selectedIndex].value;
			if(priceval.indexOf("-$") > 0){
					priceval = priceval.split("-$");
					checkpricetype = "negative";
			}else{
				if(priceval.indexOf("(-") > 0){
						priceval = priceval.split("(-");
						checkpricetype = "negative";
				}else{
					if(priceval.indexOf("+$") > 0)
						priceval = priceval.split("+$");
					else
						priceval = priceval.split("(+");
				}
			}
			
			if(priceval.length > 1){
				priceval = priceval[1].split(")");
				if(checkpricetype == "negative"){
					priceval = parseFloat("-" + priceval[0]);
					checkpricetype = "positive";
				}
				else
					priceval = parseFloat(priceval[0]);
			}else
					priceval = 0;
			
			price = price + priceval
		
		}
	}
	modPrice = modPrice + "$" + formatAsMoney(price);
    
    if(document.getElementById("price").innerHTML != modPrice) {
        document.getElementById("price").innerHTML = modPrice;

        Fader.Cycles = 3;
        Fader.Start('price','f6f8e3','cc6600')
    }
}

var Fader = new Object();

Fader.Element = null;
Fader.TimerID = null; 
Fader.Mode = 0;
Fader.Hex = 0;
Fader.Count = 0;
Fader.Delay = 50;
Fader.Cycles = 4;

Fader.Start = function (elementid,sourColor,destColor)
{   var rgb = Fader.HexToDec(sourColor);
    Fader.sR=rgb[0];
    Fader.sG=rgb[1];
    Fader.sB=rgb[2];
    
    rgb = Fader.HexToDec(destColor);
    Fader.dR=rgb[0];
    Fader.dG=rgb[1];
    Fader.dB=rgb[2];
    
    if(Fader.Element!=null)
    {   clearTimeout(Fader.TimerID);
        Fader.Reset();
    }
    
    Fader.Mode=0;
    Fader.Hex=Fader.sR;
    Fader.Count=0;
    
    Fader.Element=document.getElementById(elementid);
    Fader.Fade(Fader.sR,Fader.sG,Fader.sB);
    
}

Fader.Fade = function (r,g,b)
{   if(Fader.Mode==0)

    {   if(Fader.Hex!=Fader.dR)
        {   r = Fader.GetHex(r,Fader.sR,Fader.dR);
            g = Fader.GetHex(g,Fader.sG,Fader.dG);
            b = Fader.GetHex(b,Fader.sB,Fader.dB);
            Fader.Hex=r;
        }
        else
        {   Fader.Count+=1;Fader.Mode=1;Fader.Hex=Fader.dR;}
    }
    else
    {   if(Fader.Hex!=Fader.sR)
        {   r = Fader.GetHex(r,Fader.dR,Fader.sR);
            g = Fader.GetHex(g,Fader.dG,Fader.sG);
            b = Fader.GetHex(b,Fader.dB,Fader.sB);
            Fader.Hex=r;
        }
        else
        {   Fader.Mode=0;Fader.Hex=Fader.sR;}
    }
    
    //Fader.Element.style.border="dotted 1px rgb("+r+","+g+","+b+")"
    Fader.Element.style.color="rgb("+r+","+g+","+b+")";

    if(Fader.Count<Fader.Cycles)
        Fader.TimerID = setTimeout("Fader.Fade("+r+","+g+","+b+")",Fader.Delay);
    else
        Fader.Reset();        
}

Fader.GetHex = function (cur, sour, dest)
{   var dif

    if(cur<dest)
    {   dif = Math.round((dest-sour)/10);
        cur=((cur+dif)<dest)?(cur+dif):dest;
    }
    else
    {   dif = Math.round((sour-dest)/10);
        cur=((cur-dif)>dest)?(cur-dif):dest;
    }
        
    return cur
}

Fader.HexToDec = function (HexColor)
{   var Input = new String();
    var rgb = new Array(2);
   
    Input = HexColor + '';
   
    Input = Input.toUpperCase();

    a = Fader.GetDec(Input.substring(0, 1));
    b = Fader.GetDec(Input.substring(1, 2));
    c = Fader.GetDec(Input.substring(2, 3));
    d = Fader.GetDec(Input.substring(3, 4));
    e = Fader.GetDec(Input.substring(4, 5));
    f = Fader.GetDec(Input.substring(5, 6));

    rgb[0] = (a * 16) + b;
    rgb[1] = (c * 16) + d;
    rgb[2] = (e * 16) + f;

    return rgb
}

Fader.GetDec = function (Hex)
{
   if(Hex == "A")
      Value = 10;
   else
   if(Hex == "B")
      Value = 11;
   else
   if(Hex == "C")
      Value = 12;
   else
   if(Hex == "D")
      Value = 13;
   else
   if(Hex == "E")
      Value = 14;
   else
   if(Hex == "F")
      Value = 15;
   else
      Value = eval(Hex);

   return Value;
}

Fader.Reset = function ()
{   Fader.Element.style.background = "";  }
