/* This function is called on the buttons to call action to URL */

//		This function is being used to create the urls for
//		learn more and addtocart buttons for every offer
//		depending upon what the user clicks on.
function create_url(id, click_action) {
	if (click_action == "learnmore") {
		redir_url = "/s/landing?action=viewProduct&productId=" + id;   
	}

	if (click_action == "addtocart") {
		redir_url = "/s/cart?action=addToCart&offerId=" + id;  
	}

	if (click_action == "checkout") {
		redir_url = "/s/cart?action=viewCart";  
	}

	if (click_action == "continueshop") {
		redir_url = "/s/landing?action=viewStore";  
	}

	if (click_action == "checkoutcategory") {
		redir_url = "/s/cart?action=submitUpsell&"+id;  
	}

	if (click_action == "upsell") {
		redir_url = "/s/cart?action=showUpsells";  
	}

	document.location.href = rewrite_url(redir_url);
}

_action = ""; // action to identify which submit button has been clicked
function set_action(action) {
	_action = action;
}

function rewrite_url(url) {
	var result = url;
	result = rewrite_jsession(result);
	result = rewrite_zsession(result);
	return result;
}

function rewrite_jsession(url) {
	if (!url || !window.jsessionid || url.indexOf(";jsessionid") != -1)
		return url;

	var path = url;
	var query = '';
	var anchor = '';
	var question = url.indexOf("?");
	if (question >= 0) {
		path = url.substring(0, question);
		query = url.substring(question);
	}
	var pound = path.indexOf("#");
	if (pound >= 0) {
		anchor = path.substring(pound);
		path = path.substring(0, pound);
	}
	var result = "";
	result += path;
	if (result.length > 0) {
		result += ";jsessionid=";
		result += jsessionid;
	}
	result += anchor;
	result += query;
	return result;
}

function rewrite_zsession(url) {
	if (!url || !window.zsessionid || url.indexOf("zsessionid=") != -1)
		return url;

	var result = "";
	result += url;
	if (result.indexOf("?") == -1)
		result += "?";
	else
		result += "&";
	result += "zsessionid=";
	result += zsessionid;
	return result;
}

//Variables used in validateForm function below.
var brandstr = document.location.href;
if ( brandstr.indexOf("netzero") != -1){ brandn="HiSpeed 3G"; }
else { brandn="Turbo"; }

function validateForm(formname) {
	//get no of form elements
	var noOfElems = eval(formname + ".elements.length");
	// "Add to Cart" for upsells
	if (_action == "checkoutcategory" ) {
		var isOneChecked = false;
		var upsells = "";
		//iterate through all the form elements
		for ( var i = 0; i < noOfElems; i++ ) {
			//look for checkboxes
			if (eval(formname + ".elements[i].type") == "checkbox") {
				//see if any checkbox is selected
				if (eval(formname+".elements[i].checked")) {
					isOneChecked = true;
					upsells = upsells + "&upsellID=" + eval(formname+".elements[i].value")
				}	
			}					  
		}	
	
		if (isOneChecked) {
			create_url(upsells, "checkoutcategory");
		}
		else {
			alert("Please select a product from this page by clicking on one of the check boxes.");
			return false;
		}

	}
	
	if (_action == "addtocart") {
		var isOneChecked = false;
		var upsells = "";
		//iterate through all the form elements
		for ( var i = 0; i < noOfElems; i++ ) {
			//look for checkboxes
			if (eval(formname + ".elements[i].type") == "checkbox") {
				//see if any checkbox is selected
				if (eval(formname+".elements[i].checked")) {
					isOneChecked = true;
					upsells = upsells + "&upsellID=" + eval(formname+".elements[i].value")
						//Bug fix for Security Suite. User needs to select Turbo to purchase security suite.
						//Check if the product landing page is for Accelerator && check if Security Suite plan has been checked.
						if ( (offer_cat == "accelerator") && (eval(formname+".elements[i].value") == "ss-nis-1-95") ) { 
							//if the checkbox next to the main offer is present.
							if (document.forms[0].offerId){
								//If accelerator not checked, alert user and set focus on accelerator checkbox.
								if (!document.forms[0].offerId.checked) { document.forms[0].offerId.focus(); alert("This product is available for purchase to " + brandn + " members only."); return false; }
							}
					    }

				}	
			}					  
		}	
	
		if (!isOneChecked) {
			alert("Please select a product from this page by clicking on one of the check boxes.");
			return false;
		}

	}
	
	
	
	// "Checkout" button
	else if (_action == "checkout") {	
		create_url("", "checkout");
		return false;
	}
	// "Add to Cart" button on product landing page
	else if (_action == "addtocart") {
		return true;
	}
	return true;
}
