﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var cartStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.3",
            "background-color": "rgb(102, 102, 102)"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popup").fadeIn("slow");
		popupStatus = 1;
	}
}

function loadCart(){
	//loads popup only if it is disabled
	if(cartStatus==0){
		$("#cartBackground").css({
			"opacity": "0.3",
            "background-color": "rgb(102, 102, 102)"
		});
		$("#cartBackground").fadeIn("slow");
		$("#cart").fadeIn("slow");
		cartStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popup").fadeOut("slow");
        // Reload cart frame
		document.getElementById('cartFrame').src = document.getElementById('cartFrame').src;
		popupStatus = 0;
	}
}

function disableCart(){
	//disables popup only if it is enabled
	if(cartStatus==1){
		$("#cartBackground").fadeOut("slow");
		$("#cart").fadeOut("slow", function() {
            // Reload cart frame
            document.getElementById('cartFrame').src = document.getElementById('cartFrame').src;
        });
		cartStatus = 0;
	}
}

//centering popup
function centerPopup(){
    
    
    //$("#popup").center();
    //return;
    
    //width:386;
    //height:493;
    
    $("#popup").css({
      "position": "fixed",
      "top": "50%",
      "left": "50%",
      "width": "623px",
      "height": "425px",
      "margin-top": "-212px", /* Half the height */
      "margin-left": "-311px" /* Half the width */
    });
    
    //$("#popup").center();
    
    
    
    
    /*
	//request data for centering
	var windowWidth = window.innerWidth + window.pageXOffset;
	var windowHeight = window.innerHeight + window.pageYOffset * 2;
	
    if(isNaN(windowHeight)){
        windowHeight = document.documentElement.clientHeight + document.documentElement.scrollLeft;
    }
    
    if(isNaN(windowWidth)){
        windowWidth = document.documentElement.clientWidth + document.documentElement.scrollTop * 2;
    }
    
    var popupHeight = $("#popup").height();
	var popupWidth = $("#popup").width();
    
    var calcHeight = (windowHeight / 2) - (popupHeight / 2);
    var calcWidth = (windowWidth / 2) - (popupWidth / 2);
    
	//centering
	$("#popup").css({
		"position": "absolute",
        "top": calcHeight,
        "left": calcWidth,
        "margin": "0 auto"
	});
    
    $("#backgroundPopup").css({
		"height": windowHeight
	});
    */

}



jQuery.fn.center = function () {  
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.outerHeight() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.outerWidth() ) / 2+$(window).scrollLeft() + "px");
    return this;
}


/*
jQuery.fn.center = function () { 
     
      this.CenterIt();
}
*/


function centerCart(){
    
    
    $("#cart").css({
      "position": "fixed",
      "top": "50%",
      "left": "50%",
      "width": "386px",
      "height": "493px",
      "margin-top": "-246px", /* Half the height */
      "margin-left": "-193px" /* Half the width */
    });
    
    
    //$("#cart").center();
    
    
    /*
	//request data for centering
    var windowWidth = window.innerWidth + window.pageXOffset;
    var windowHeight = window.innerHeight + window.pageYOffset * 2;
    
    if(isNaN(windowHeight)){
        windowHeight = document.documentElement.clientHeight + document.documentElement.scrollLeft;
    }
    
    if(isNaN(windowWidth)){
        windowWidth = document.documentElement.clientWidth + document.documentElement.scrollTop * 2;
    }
    
	var popupHeight = $("#cart").height();
	var popupWidth = $("#cart").width();
    
    var calcHeight = (windowHeight / 2) - (popupHeight / 2);
    var calcWidth = (windowWidth / 2) - (popupWidth / 2);
    
    
	//centering
	$("#cart").css({
		"position": "absolute",
		"top": calcHeight,
		"left": calcWidth
	});
	//only need force for IE6
	
	$("#cartBackground").css({
		"height": windowHeight
	});
    */
	
}

function loadGatewayType(typeId) {
    $('#popupFrame').attr('src', 'includes/gateway.php?gateway=' + typeId);
}

function loadGatewayItem(itemId) {
    $('#popupFrame').attr('src', 'includes/gateway.php?itemId=' + itemId);
}

function deleteItem(item) {
    var id = item.attr('name');
    
    // Delete from session via ajax, then remove the html element
    $.getJSON("posts/delete_item.php?id="+id, function(data) {
        // If successfully deleted
        if (data) {
            parent.DecrementItemCount()
            item.remove();
        }
        else
            alert('There was a problem deleting this item');
    });
}

function swapToCart() {
    disablePopup();
    
    centerCart();
    loadCart();
}

function swapToEditItem(itemId) {
    disableCart();
    
    loadGatewayItem(itemId);
    centerPopup();
    loadPopup();
}

function editItem(item) {
    var id = item.attr('name');

    parent.swapToEditItem(id);
}


function showItemPopup(obj) {
    
    loadGatewayType($(obj).attr('rel'));
    centerPopup();
    loadPopup();    
}

function setSizeValue(value) {
    
    //$("#size-button").val(value);
    //$.fancybox.close()
    
    $("#size-button", frames['popupFrame'].document).val(value);
    $("#size_popup").fadeOut("slow");
}

function loadSizePopup() {
    
    var popupId = $("#size-button").attr('popup-id');
    
    $('#fancybox-outer').replaceWith($(popupId).clone());
}



function showSizePopup() {    

    var popup_id = $("#size-button").attr('popup-id');
    //alert(popup_id);
    centerSizePopup();

    $("#size_popup_content", window.parent.document).empty();
    //$("#size_popup_content", window.parent.document).append($(popup_id).clone());
    
    var new_size_popup = $(popup_id).clone().html();
    
    var popup_name = popup_id.replace('#', '');
    
    //alert(new_size_popup.show());
    $("#size_popup_content", window.parent.document).append('<div id="' + popup_name + '" class="popup-size">' + new_size_popup + '</div>');
    //$("#size_popup_content", window.parent.document).append('<div id="popup-size-26" class="popup-size"></div>');
    //$(popup_id).appendTo("#size_popup_content", window.parent.document);
    $("#size_popup", window.parent.document).fadeIn("slow");
 
     
}

function centerSizePopup(){

    $("#size_popup", window.parent.document).css({
      "position": "fixed",
      "top": "50%",
      "left": "50%",
      "width": "950px",
      "height": "651px",
      "margin-top": "-325px", /* Half the height */
      "margin-left": "-475px" /* Half the width */
    });
    
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
 
     // Signage selector popup
    /*$("#size-button").fancybox({
         modal: true,
         autoScale: false,
         
         href: $("#size-button").attr('popup-id'),
         onStart: loadSizePopup
    });*/
    $("#size-button").click(function(){
        showSizePopup();
    }); 
    
    
    /* Uncomment to enable fancy select boxes */
    //$('select').sSelect();
    
    
		/*$("input[name='form_design_type']").change(function(){
			$(".on-radio-button-selected").attr('class','on-radio-button');
			var n = $("input[name='form_design_type']:checked").attr('id');
			$('#' + n + '_image').attr('class','on-radio-button-selected');
		});*/
	
    // Change focus to document on combo box selection
    $('.newListSelected').click( function() {
        $(document).focus();
    });
    
	$(".blocker").click(function(){
		
        //loadGatewayType($(this).attr('rel'));
        //centerPopup();
		//loadPopup();
		
        showItemPopup(this);
        
        
        //document.getElementById('gv').value = $(this).attr('rel');
		//setTimeout("document.getElementById('gatewayForm').submit()",0);
	});
	
	$("#view_cart").click(function(){
		centerCart();
		loadCart();
	});
    
    // Click on item in the cart
    $(".item-cart").click(function() {
        // Get the actual item div
        var item = $(this).parent();
        editItem(item);
    });
        
    // Click on the delete item in the cart
    $(".item-delete").click(function() {
        // Get the actual item div
        var item = $(this).parent();
        deleteItem(item);
    });
    
    $(this).ajaxError(function(event, XMLHttpRequest, ajaxOptions, thrownError) {
        alert("Error requesting page " + ajaxOptions.url + "\n" + thrownError);
    });
	
    /*
	$(".pay_now").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
		setTimeout("document.getElementById('mainForm').submit()",2000);
	});
    */
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
});

	$(document).ready(function(){
		
	});
	function fmoney(mnt) {
	mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);

}
	function setDesignCost(cost) {
		document.getElementById('order_total_design_value').innerHTML = '$' + fmoney(cost);
		document.getElementById('design_value').value = cost;
		updateTotal();
	}
	
	function setFreightCost(cost) {
		document.getElementById('order_total_freight_value').innerHTML = '$' + fmoney(cost);
		document.getElementById('freight_value').value = cost;
		updateTotal();
	}
	
	function updateTotal() {
		var cost = Number(document.getElementById('freight_value').value) + Number(document.getElementById('design_value').value) + Number(document.getElementById('job_value').value);
		document.getElementById('payable_value').value = cost;
		document.getElementById('order_total_payable_value').innerHTML = '$' + fmoney(cost);
	}
	
	function setContactMethod(method) {
		document.getElementById('form_contact_method').value = method;
		/*if (method == 'call') {
			document.getElementById('order_call_me_btn').style.background = "url(images/call_select.png) no-repeat";
			document.getElementById('order_email_me_btn').style.background = "url(images/email.png) no-repeat";
			document.getElementById('order_both_btn').style.background = "url(images/both.png) no-repeat";
		} else if (method == 'email') {
			document.getElementById('order_email_me_btn').style.background = "url(images/email_select.png) no-repeat";
			document.getElementById('order_call_me_btn').style.background = "url(images/call.png) no-repeat";
			document.getElementById('order_both_btn').style.background = "url(images/both.png) no-repeat";
		} else {
			document.getElementById('order_both_btn').style.background = "url(images/both_select.png) no-repeat";
			document.getElementById('order_email_me_btn').style.background = "url(images/email.png) no-repeat";
			document.getElementById('order_call_me_btn').style.background = "url(images/call.png) no-repeat";
		}*/
        
        if (method == 'call') {
            document.getElementById('order_call_me_btn').style.backgroundPosition = "left bottom";
            document.getElementById('order_email_me_btn').style.backgroundPosition = "left top";
            document.getElementById('order_both_btn').style.backgroundPosition = "left top";
        } else if (method == 'email') {
            document.getElementById('order_email_me_btn').style.backgroundPosition = "left bottom";
            document.getElementById('order_call_me_btn').style.backgroundPosition = "left top";
            document.getElementById('order_both_btn').style.backgroundPosition = "left top";
        } else {
            document.getElementById('order_both_btn').style.backgroundPosition = "left bottom";
            document.getElementById('order_call_me_btn').style.backgroundPosition = "left top";
            document.getElementById('order_email_me_btn').style.backgroundPosition = "left top";
        }
    
	}
	
	function setContactMethod2(method) {
		document.getElementById('form_contact_method').value = method;
		
        /*if (method == 'call') {
			document.getElementById('order_call_me_btn_cart').style.background = "url(../images/call_select.png) no-repeat";
			document.getElementById('order_email_me_btn_cart').style.background = "url(../images/email.png) no-repeat";
			document.getElementById('order_both_btn_cart').style.background = "url(../images/both.png) no-repeat";
		} else if (method == 'email') {
			document.getElementById('order_email_me_btn_cart').style.background = "url(../images/email_select.png) no-repeat";
			document.getElementById('order_call_me_btn_cart').style.background = "url(../images/call.png) no-repeat";
			document.getElementById('order_both_btn_cart').style.background = "url(../images/both.png) no-repeat";
		} else {
			document.getElementById('order_both_btn_cart').style.background = "url(../images/both_select.png) no-repeat";
			document.getElementById('order_email_me_btn_cart').style.background = "url(../images/email.png) no-repeat";
			document.getElementById('order_call_me_btn_cart').style.background = "url(../images/call.png) no-repeat";
		}*/
        
        submitQuote();
	}
    
    
    function submitQuote() {
        
        var order_contact_method = document.getElementById('form_contact_method').value;
    
        if(order_contact_method != 'call' && order_contact_method != 'email' && order_contact_method !='both'){
            alert('Please select how you would like us to contact you');
            return false;
        }
        
        
        if(validateQuoteForm())
        {
            document.getElementById('cartForm').submit();    
        }
        
    }
    
    
    var reqQuoteFields = new Array();
    reqQuoteFields['form_client_name'] = 'Client Name';
    reqQuoteFields['form_email'] = 'Email';
    reqQuoteFields['form_phone'] = 'Telephone';
    reqQuoteFields['form_area_code'] = 'Area Code';
    reqQuoteFields['form_time_to_call'] = 'Best time to call';
    

    function validateQuoteForm(){
        elems = document.getElementById('cartForm').elements;
        var ret = Array();
        var error = new Array();
        for (var i=0;i<elems.length;i++) {
            if (typeof reqQuoteFields[elems[i].name] != 'undefined') {
                if (elems[i].type == 'select-one') {
                    if (elems[i].options[elems[i].selectedIndex].value == '') {
                            error.push(reqQuoteFields[elems[i].name]);
                    }
                } else {
                    if (elems[i].value == '') {
                        error.push(reqQuoteFields[elems[i].id]);
                    }
                }
            }
            ret.push(elems[i].name+'='+elems[i].value);
        }
        if (error.length == 0) {
            //return ret.join('&')
            return true;
        } else {
            alert("Submission Error - The following fields require data.\n\nPlease correct the following:\n  - "+error.join("\n  - "));
            //alert("Please fill in all fields");
            return false;
        }
    }
    
	
	function goHome() {
		disablePopup();
		setTimeout("window.location = 'index.php';",1000);
	}

