﻿var ApplicationPath = '/APAStore';

/* Functions for string manipulation ********************************************/
/********************************************************************************/
	function replace(str, original, replacement)
	{
		var result = '';
		while (str.indexOf(original) != -1) {
			if (str.indexOf(original) > 0) {
				result = result + str.substring(0, str.indexOf(original)) + replacement;
			} else {
				result = result + replacement;
			}
			
			str = str.substring(str.indexOf(original) + original.length, str.length);
		}
		return result + str;
	}
	
	function Trim(TRIM_VALUE)
	{
		if (TRIM_VALUE.length < 1) { return ""; }
		
		TRIM_VALUE = RTrim(TRIM_VALUE);
		TRIM_VALUE = LTrim(TRIM_VALUE);
		if (TRIM_VALUE=="") { 
			return "";
		} else {
			return TRIM_VALUE;
		}
	}

	function RTrim(VALUE)
	{
		var w_space		= String.fromCharCode(32);
		var v_length	= VALUE.length;
		var strTemp		= "";
		
		if(v_length < 0) { return ""; }
		
		var	iTemp = v_length -1;
		while (iTemp > -1) {
			if (VALUE.charAt(iTemp) == w_space) {
			} else {
				strTemp = VALUE.substring(0,iTemp +1);
				break;
			}
			iTemp = iTemp-1;
		}
		return strTemp;
	}
	
	function LTrim(VALUE){
		var w_space = String.fromCharCode(32);
		if (v_length < 1) { return ""; }
		
		var v_length	= VALUE.length;
		var strTemp		= "";
		var iTemp		= 0;

		while (iTemp < v_length) {
			if (VALUE.charAt(iTemp) == w_space) {
			} else {
				strTemp = VALUE.substring(iTemp,v_length);
			break;
			}
			iTemp = iTemp + 1;
		}
		return strTemp;
	}
	
	function FormatCurrency(amount)
	{
		var i = parseFloat(amount);
		
		if (isNaN(i)) { i = 0.00; }
		
		var minus = '';
		if (i < 0) { minus = '-'; }
		i = Math.abs(i);
		i = parseInt((i + .005) * 100);
		i = i / 100;
		
		var s = new String(i);
		if (s.indexOf('.') < 0) { s += '.00'; }
		if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
		s = minus + s;
		return '$'+s;
	}
	
/********************************************************************************/

/* Functions for parsing and getting querystring values *************************/
/********************************************************************************/
	function PageQuery(q)
	{
		if (q.length > 1) { 
			this.q = q.substring(1, q.length);
		} else {
			this.q = null;
		}
		
		this.keyValuePairs = new Array();
		if (q) {
			for (var i=0; i < this.q.split("&").length; i++) {
				this.keyValuePairs[i] = this.q.split("&")[i];
			}
		}
		
		this.getKeyValuePairs = function() {
			return this.keyValuePairs;
		}
		
		this.getValue = function(s) {
			for(var j=0; j < this.keyValuePairs.length; j++) {
				if (this.keyValuePairs[j].split("=")[0] == s) {
					return this.keyValuePairs[j].split("=")[1];
				}
			}
			return false;
		}
		
		this.getParameters = function() {
			var a = new Array(this.getLength());
			for (var j=0; j < this.keyValuePairs.length; j++) {
				a[j] = this.keyValuePairs[j].split("=")[0];
			}
			return a;
		}
		
		this.getLength = function() {
			return this.keyValuePairs.length;
		}
	}
	
	function queryString(key) {
		var page	= new PageQuery(window.location.search);
		return unescape(page.getValue(key));
	}
/********************************************************************************/

/* Functions for handling key-specific events ***********************************/
/********************************************************************************/
	function handleEnter(elementIdToFocus, e) {
	    var keyPressed;
	    var notIE = 0;
	    // for IE
	    if(window.event) {
	        keyPressed = e.keyCode;
	        if(/WebKit/i.test(navigator.userAgent)) {
	           notIE = 1;
	           //alert("safari");
	        }
	    }
	    else if(e && e.which) {
	        keyPressed = e.which;
	        notIE = 1;
	    }
	    else {
	        return true;
	    }
	    //e.stop();
		if (keyPressed == 13) {
			//if(notIE == 0) {
    		//	document.getElementById(elementIdToFocus).focus();
    		//}
			document.getElementById(elementIdToFocus).click();
			Event.stop(e);
			//if(notIE == 1) {
			  //alert("not IE");
			 // return false;
			//}
		}
	}
	
/********************************************************************************/

/* Functions for toggling element visibility ************************************/
/********************************************************************************/
	function _hide(tid) {
		if (tid && tid != "") {
			var obj = document.getElementById(tid);
			if (obj) {
				obj.style.display = "none";	
			}
		} else {
			return;
		}
	}

	function _show(tid) {
		if (tid && tid != "") {
			var obj = document.getElementById(tid);	
			if (obj) {
				obj.style.display = "block";	
			}	
		} else {
			return;
		}
	}

	function Toggle(elementId)
	{
		if (elementId && elementId != "") {
			var element = document.getElementById(elementId);
			element.style.display = (element.style.display == 'none' || element.style.display == null) ? 'block' : 'none';
		} else {
			return false;
		}
	}
	
	function ToggleWithLabel(elementId, elementLabel) {
		Toggle(elementId);
		
		var	element	= document.getElementById(elementId);
		var label	= document.getElementById(elementLabel);
		
		label.innerHTML = (element.style.display == 'none') ? 'See' : 'Hide';
	}
	
/********************************************************************************/

/* Functions for toggling element visibility on browse page**********************/
/********************************************************************************/

function toggleSubList(link) {
    var linkText = link.firstChild;
    if(linkText.getAttribute("src").indexOf("plus.gif") != -1) linkText.setAttribute("src", ApplicationPath + "/lib/css/img/minus.gif");
    else linkText.setAttribute("src", ApplicationPath + "/lib/css/img/plus.gif");
    
    var targetList = link.nextSibling.nextSibling.nextSibling.nextSibling;
    if(targetList.className == "hideMe") targetList.className = "";
    else targetList.className = "hideMe";
    }
	
/********************************************************************************/

/*** BEGIN AJAX FUNCTIONS ***/

	/* Functions for user authentication ********************************************/
	/********************************************************************************/
		// Called from /customer/login.ascx
		function SignIn(eLoginFormID, eLoginFormPwd)
		{
			var	txtLoginFormID	= document.getElementById(eLoginFormID);
			var	txtLoginFormPwd	= document.getElementById(eLoginFormPwd);
			
			if (txtLoginFormID.value.trim() != '' && txtLoginFormPwd.value.trim() != '')
			{
				_hide('plcLoginForm');
				_show('plcLoginFormProcessing');
				
				APA.Store.UI.Customer.clsAccountLogin.SignIn(txtLoginFormID.value, txtLoginFormPwd.value, _callback_SignIn);
			}
			else
			{
				_show('LoginFormMsg');
				document.getElementById('LoginFormMsg').innerHTML = 'Please enter your ID or E-mail and Password';
			}
		}
			function _callback_SignIn(response) {
				if (response.value == true) {
					document.location.href = document.location.href;
				} else {
					
					_show('plcLoginForm');
					_hide('plcLoginFormProcessing');
					_show('LoginFormMsg');
					
					document.forms[0].reset();
				}
			}
	/********************************************************************************/
	
	/* Functions for Cart & Wishlist additions **************************************/
	/********************************************************************************/
		var prod_id;
		var prod_name;
		
		function ShowLicense(id) {
			document.location.href = ApplicationPath + '/Order/license.aspx?p=' + id;
		}
		function _callback_ShowLicense(response) {
			if (response.value == true) {
					document.location.href = ApplicationPath + '/Order/license.aspx?p=' + id;
			} else {
				alert(response);
			}
		}
			
		function AddToCart(id, code, name, price, quantity, requiresLicense, isNonInventory) {
			prod_id		= id;
			prod_name	= name;
			
			APA.Store.UI.Templates.clsStoreBasePage.AddToCart(id, code, name, price, quantity, requiresLicense, isNonInventory, _callback_AddToCart);
		}
			function _callback_AddToCart(response) {
				if (response.value == true) {
					//if ( confirm('\"' + prod_name + '\" was added to your Shopping Cart.\nWould you like to Check Out now?') ) {
						document.location.href = ApplicationPath + '/Order/Default.aspx';
					//}
				} else {
					alert(response);
				}
			}
			
		function AddToWishList(id, name) {
			prod_id		= id;
			prod_name	= name;
			
			APA.Store.UI.Templates.clsStoreBasePage.AddToWishList(id, _callback_AddToWishList);	
		}
			function _callback_AddToWishList(response) {
				if (response.value == false) {
					document.location.href = ApplicationPath + '/Customer/Wishlist.aspx?p=' + prod_id;
				} else {
					//if ( confirm('\"' + prod_name + '\" was added to your Wish List.\nView complete Wish List?') ) {
						document.location.href = ApplicationPath + '/Customer/Wishlist.aspx';
					//}
				}
			}
	/********************************************************************************/

/*** END AJAX FUNCTIONS ***/
	
	function bodyOnload()
	{
		if (document.location.href.toLowerCase().indexOf('/order/shipping.aspx') > 0) {
			GetShipMethods();
		}
	}