/**
 * @author Gproxy Design Inc.
 * @copyright (c) 2009, Gproxy Design Inc. All rights reserved.
 * @version 2.5
 *
 * Agreement:
 * Redistribution and use of this software in source and binary forms, with or
 * without modification, are permitted provided that all of the following
 * conditions are met:
 * (1) Redistributions of source code must retain:
 * 	(i) the above copyright notice,
 *	(ii) this list of redistribution conditions, and
 *	(iii) the disclaimer appearing below.
 *	(iiii) the logos, hyperlinks or publicity provided in the original version.
 * (2) Redistributions in binary form must retain:
 *	(i) the above copyright notice,
 *	(ii) this list of redistribution conditions, and
 *	(iii) the disclaimer appearing below in the documentation and/or any other materials provided with the distribution.
 *	(iiii) the logos, hyperlinks or publicity provided in the original version.
 * (3) Neither the name of the Gproxy Design nor the names of its contributors may be used to endorse or promote
 *     products derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * DESCRIPTION
 *
 * Company  Gproxy Design Inc - www.gproxy.com
 *
 * Name of Script (Wish List Main.js)
 *
 * Date   08/08/2008
 *
 * Type   Java Script
 *
 * Description: Gives the Wish List functionality all the main functions to work
 *
 */
/**
 * WHIS LIST
 * Adds the item to wish list when the customer it's not loggued
 * Recives the customer internal id
 * @param {Object} customerId
 */
function wlsAddItemCookie(customerId, siteNumber){
    var itemId = wlsReadCookie('itemId');
    if (itemId != null && itemId != "") {
        wlsEraseCookie('itemId');
        var src = '/app/site/hosting/scriptlet.nl?script=customscript_wls_additem&deploy=customdeploy_wls_additem&customerid=' + customerId + '&sitenumber=' + siteNumber + '&itemid=' + itemId;
        var scriptFile = document.createElement('script');
        scriptFile.setAttribute('type', 'text/javascript');
        scriptFile.setAttribute('src', src);
        document.getElementsByTagName('head')[0].appendChild(scriptFile);
    }
}

/**
 * WHIS LIST
 * Adds the item to the customer wish list, the customer is already logged
 * Recives the customer internal id and the item internal id
 * @param {Object} itemId
 * @param {Object} customerId
 * @param (Object) loginUrl
 */
function wlsAddItem(customerId, siteNumber, itemId, loginUrl){
    if (customerId == "") {
        wlsCreateCookie('itemId', itemId, 10);
        window.location.href = loginUrl;
    }
    else {
        var src = '/app/site/hosting/scriptlet.nl?script=customscript_wls_additem&deploy=customdeploy_wls_additem&customerid=' + customerId + '&sitenumber=' + siteNumber + '&itemid=' + itemId;
        var scriptFile = document.createElement('script');
        scriptFile.setAttribute('type', 'text/javascript');
        scriptFile.setAttribute('src', src);
        document.getElementsByTagName('head')[0].appendChild(scriptFile);
    }
}

/**
 * WISH LIST
 * @param {Object} customerId
 * @param {Object} itemId
 */
function wlsRemoveItem(customerId, siteNumber, itemId){
    var src = '/app/site/hosting/scriptlet.nl?script=customscript_wls_removeitem&deploy=customdeploy_wls_removeitem&customerid=' + customerId + '&sitenumber=' + siteNumber + '&itemid=' + itemId;
    var scriptFile = document.createElement('script');
    scriptFile.setAttribute('type', 'text/javascript');
    scriptFile.setAttribute('src', src);
    document.getElementsByTagName('head')[0].appendChild(scriptFile);
}

/**
 * WISH LIST
 * @param {Object} customerId
 */
function wlsClearItems(customerId, siteNumber){
    var src = '/app/site/hosting/scriptlet.nl?script=customscript_wls_clearitems&deploy=customdeploy_wls_clearitems&customerid=' + customerId + '&sitenumber=' + siteNumber;
    var scriptFile = document.createElement('script');
    scriptFile.setAttribute('type', 'text/javascript');
    scriptFile.setAttribute('src', src);
    document.getElementsByTagName('head')[0].appendChild(scriptFile);
}

/**
 * WISH LIST
 * Shows the items in the Customer Wishlist
 * Recives the Customer Id and the login url of the site
 * @param {Object} customerId
 * @param (Object) loginUrl
 */
function wlsShowItems(customerId, siteNumber, loginUrl){
    if (customerId == '') {
        //Redirect to login page
        window.location.href = loginUrl;
    }
    else {
        var src = '/app/site/hosting/scriptlet.nl?script=customscript_wls_getitems&deploy=customdeploy_wls_getitems&display=wishlist_items&customerid=' + customerId + '&sitenumber=' + siteNumber;
        var scriptFile = document.createElement('script');
        scriptFile.setAttribute('type', 'text/javascript');
        scriptFile.setAttribute('src', src);
        document.getElementsByTagName('head')[0].appendChild(scriptFile);
    }
}


/**
 * WISH LIST
 * Adds a new item to the cart
 * @param {Object} itemId
 */
function wlsAddToCart(itemId){    
	if (document.forms['form'+itemId].qty.value > 0 ) {	
    	document.forms['form'+itemId].submit();    	    		
	}
	else{
		document.forms['form'+itemId].qty.value = '1';
		alert('Invalid number (must be greater than 0).');		
	}    
}

/**
 * WISH LIST
 * Opens the Gproxy page
 */
function wlsRedirectGProxy(){
    window.open('http://www.gproxy.com');
}

/**
 * COOKIES
 * Creates a new cookie in the browser
 * Recives the name, the value and when expires the cookie
 * @param {Object} name
 * @param {Object} value
 * @param {Object} days
 */
function wlsCreateCookie(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 + "=" + escape(value) + expires + "; path=/";
}

/**
 * COOKIES
 * Reads a cookie
 * Recives the name for the cookie
 * @param {Object} name
 */
function wlsReadCookie(name){
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(name + "=");
        if (c_start != -1) {
            c_start = c_start + name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) 
                c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return null;
}

/**
 * COOKIES
 * Erase a cookie
 * Recives the name for the cookie
 * @param {Object} name
 */
function wlsEraseCookie(name){
    wlsCreateCookie(name, "", 10);
}

/**
 * EVENTS
 * Add events to an html objects
 * Recives the html object, the type of the event, the function and the capture
 * @param {Object} obj
 * @param {Object} evType
 * @param {Object} fn
 * @param {Object} useCapture
 */
function wlsAddEvent(obj, evType, fn, useCapture){
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, useCapture);
        return true;
    }
    else 
        if (obj.attachEvent) {
            return (obj.attachEvent("on" + evType, fn));
        }
        else 
            return false;
}

