﻿// JScript File

var MAX_COMPARISON_COUNT = 10;

function cartProductQtyChange(elem)
{
	var product_id = elem.id.replace("qty_product_", "");
	var product_qty = elem.options[elem.selectedIndex].value;
	var warranty_select = document.getElementById("qty_warranty_" + product_id)
	if (warranty_select != null)
	{
		for (var i = warranty_select.length - 1; i >= 0; i--)
		{
			warranty_select.remove(0);
		}

		for (var i = 0; i < product_qty; i++)
		{
			var y = document.createElement('option');
			y.value = (i + 1);
			y.text = (i + 1);
			try
			{
				warranty_select.add(y, null); // standards compliant
			}
			catch (ex)
			{
				warranty_select.add(y); // IE only
			}
		}
	}
}

/**
*	called to insert an item to cart
*/
function B2C_SubmitToCart(txtQtyId, productId)
{
	if (!isInteger(productId))
	{
		return;
	}
	
	if (document.getElementById(txtQtyId))
	{
		var qty = document.getElementById(txtQtyId).value;
		if (!isInteger(qty))
		{
			qty = 1;
		}
		
		if (parseInt(qty) > 20)
		{
			alert("Quantity cannot be greater than 20");
			return;
		}

		location.href = B2C_Host_R + "Cart/Default.aspx?formAction=add&productId=" + productId + "&qty=" + qty;
	}
	else
	{
		alert("Quantity must be greater than 0...")
		return;
	}
} //end of submitToCart

/**
* called to remove an item from cart
*/
function B2C_RemoveCartItem(productId)
{
	if (!isInteger(productId))
	{
		location.href = B2C_Host_R + "Cart/Default.aspx?formAction=remove&productId=" + productId;
	}
} //end of B2C_RemoveCartItem


/**
*
*/
function addWarrantyToCart(category_id, product_id, menuId, parentId1, parentId2, parentId3, menuLevel, warranty_product_select, cart_itm_grpd_idx)
{

	var obj = document.getElementById(warranty_product_select);
	warranty_product_id = obj.options[obj.selectedIndex].value;
	warranty_product_name = obj.options[obj.selectedIndex].text;


	if (!isNumber(warranty_product_id))
	{
		return;
	}
	
	var qty = 1;
	if (!document.getElementById("qty_warranty_" + cart_itm_grpd_idx))
	{
	//        return;
	}
	else
	{
		var qty_box = document.getElementById("qty_warranty_" + cart_itm_grpd_idx);
		var qty = qty_box.value;
	}
	
	if (!isNumber(qty))
	{
		alert("Invalid Quantity Amount...")
	}
	else
	{
		//vaildate qty amount, must be <= 20
		var regex = /^([1-9]|1[1-9]|2[0])$/;
		if (!regex.test(qty))
		{
			alert("Quantity cannot be greater than 20");
			return;
		}

		if (parseInt(qty) > 0)
		{
			var url = brand2media_host + "catalogue/cart/Default.aspx";
			url += "?categoryId=" + category_id + "&productId=" + warranty_product_id + "&qty=" + qty;
			if (menuId != null && menuId != "") url += "&menuId=" + menuId;
			if (parentId1 != null && parentId1 != "") url += "&parentId1=" + parentId1;
			if (parentId2 != null && parentId2 != "") url += "&parentId2=" + parentId2;
			if (parentId3 != null && parentId3 != "") url += "&parentId3=" + parentId3;
			if (menuLevel != null && menuLevel != "") url += "&menuLevel=" + menuLevel;
			url += "&formAction=add_item";
			url += "&returnCart=1";
			url += "&warrantyProductId=" + warranty_product_id;
			url += "&parentProductId=" + product_id;
			url += "&warrantyName=" + warranty_product_name;
			url += "&targetCartItmIdx=" + cart_itm_grpd_idx;
			//alert(url);
			//return;
			location.href = url;
		}
		else
		{
			alert("Quantity must be greater than 0...")
		}
	}
} //end of submitToCart


/**
* author: Rufin
*/
function submitToCartBundleProduct(category_id, target_product_id, bundle_product_id, product_id, menuId, parentId1, parentId2, parentId3, menuLevel)
{

	if (!isNumber(product_id))
	{
		return;
	}
	var qty = 1;
	if (!document.getElementById("qty_" + product_id))
	{
		//        return;
	}
	else
	{
		var qty_box = document.getElementById("qty_" + product_id);
		var qty = qty_box.value;
	}
	if (!isNumber(qty))
	{
		alert("Invalid Quantity Amount...")
	}
	else
	{
		//vaildate qty amount, must be <= 20
		var regex = /^([1-9]|1[1-9]|2[0])$/;
		if (!regex.test(qty))
		{
			alert("Quantity cannot be greater than 20");
			return;
		}

		if (parseInt(qty) > 0)
		{
			var url = brand2media_host + "catalogue/cart/Default.aspx";
			url += "?categoryId=" + category_id + "&targetProductId=" + target_product_id + "&bundleProductId=" + bundle_product_id + "&productId=" + product_id + "&qty=" + qty;
			if (menuId != null && menuId != "") url += "&menuId=" + menuId;
			if (parentId1 != null && parentId1 != "") url += "&parentId1=" + parentId1;
			if (parentId2 != null && parentId2 != "") url += "&parentId2=" + parentId2;
			if (parentId3 != null && parentId3 != "") url += "&parentId3=" + parentId3;
			if (menuLevel != null && menuLevel != "") url += "&menuLevel=" + menuLevel;
			url += "&formAction=add_item";
			url += "&returnCart=1";

			location.href = url;
		}
		else
		{
			alert("Quantity must be greater than 0...")
		}
	}
} //end of submitToCart



//load product manual pdf file
function pm_showManualPDF(url)
{
	var w = 800;
	var h = 600;
	var leftPoint = (top.screen.width - w) / 2;
	var topPoint = (top.screen.height - h) / 2;

	url = "http://localhost/VisionsInfo/manuals/" + url + ".pdf";  //local    
	//    url = "http://www.visions.ca/manuals/" + url + ".pdf";  //live
	//    url = "http://sec01.visionsadmin.com/StagingInfo.visions.ca/staging_manuals/" + url + ".pdf";  //staging
	detailWindow(url, w, h);
} //end of loadFilePDF  

/*=========================================================================================
/*	product comparison function
/*=========================================================================================*/
/**
* author: sherwin
* revised version of buildCompareList
* date: 2009-04-29
* PARAMS:
*	check_box:	checkbox that being clicked
*	elemId:		html element that holds productId
*	hidCompareListId:	html hidden field id that holds list of products for comparison
*/
function B2C_BuildCompareList(check_box, elemId, hidCompareListId)
{
	var compare_list = document.getElementById(hidCompareListId);
	var element = document.getElementById(elemId);
	if (compare_list && element)
	{
		var compare_list_value = compare_list.value;
		var product_id = element.value;
		var compare_product_id = "|" + product_id + "|";


		if (check_box.checked)
		{
			if (isComparisonProductExist(hidCompareListId, compare_product_id))
			{
				return;
			}

			if (isMaxComparisonCount(hidCompareListId))
			{
				if (trim(compare_list_value) != "")
				{
					compare_list_value += ",";
				}
				compare_list_value += compare_product_id;
			}
			else
			{
				check_box.checked = false;
			}
		}
		else
		{
			compare_list_value = compare_list_value.replace(compare_product_id + ",", "");
			compare_list_value = compare_list_value.replace("," + compare_product_id, "");
			compare_list_value = compare_list_value.replace(compare_product_id, "");
		}
		compare_list.value = compare_list_value;
		//        document.getElementById("debug").innerHTML = compare_list_value.replace(",", ",<br />");
	}
} //end of B2C_BuildCompareList

/**
* author: sherwin
*/
function isMaxComparisonCount(hidCompareListId)
{
	var compare_list = document.getElementById(hidCompareListId);
	if (compare_list)
	{
		var compare_list_value = compare_list.value;
		var compare_list_arr = compare_list_value.split(',');
		if (compare_list_arr.length == MAX_COMPARISON_COUNT)
		{
			alert("Max number of Products to Compare has been selected.\n\nPlease click the button \"Compare\" to view the comparison");
			return false;
		}
	}
	return true;
} //end of isMaxComparisonCount


function isComparisonProductExist(hidCompareListId, product_id)
{
	var flag = false;
	var compare_list_value = document.getElementById(hidCompareListId).value;
	//    alert(compare_list_value + "\nproduct_id=" + product_id + "\ncompare_list_value.indexOf(product_id): " + compare_list_value.indexOf(product_id));
	if (compare_list_value.indexOf(product_id) >= 0)
	{
		flag = true;
	}

	return flag;
}

/**
* 
*/
function PC_ShowCompare(category_id, hidCompareListId)
{
	var compare_list = document.getElementById(hidCompareListId);
	var url = B2C_Host_R + "catalogue/compare/Compare.aspx?categoryId=" + category_id;
    if (compare_list)
    {
        url += "&chkCompareProducts=" + compare_list.value;
    }
    
	if (window.opener != null)
	{
	    window.opener = self;
        location.href = url
	}
    else
    {
        var newwin = detailWindow(url, 800, 600);
    }
} //end of PC_ShowCompare

function PC_RemoveItem(elemId, product_id)
{
    var compare_list = document.getElementById(elemId);
    if (isNumber(product_id) && compare_list)
    {
        var compare_list_value = compare_list.value;
        var compare_product_id = "|" + product_id + "|";
        
        compare_list_value = compare_list_value.replace(compare_product_id + ",", "");
        compare_list_value = compare_list_value.replace("," + compare_product_id, "");
        compare_list_value = compare_list_value.replace(compare_product_id, "");
        compare_list.value = compare_list_value;
        
        B2C_Form.submit();
    }
}//end of PC_RemoveItem

function PC_ProcessProductDetail(categoryId, productId)
{
	if (isNumber(productId))
    {
    	var url = B2C_Host_R + "Catalogue/Category/Details.aspx?categoryId=" + categoryId + "&productId=" + productId;      
        window.opener.location.href = url;
        opener.focus();
    }
}//end of PC_ProcessProductDetail

function PC_ProcessAddToCart(categoryId, productId)
{
	if (!isNumber(productId))
    {
        return;
    }
    
    var qty = 1;    
    var url = B2C_Host_R + "Cart/Default.aspx";
    url += "?formAction=add&categoryId=" + categoryId + "&productId=" + productId + "&qty=" + qty;
    
    window.opener.location.href = url;
    opener.focus();
  
}//end of PC_ProcessAddToCart

function PC_ShowFeatureGlossaryWindow(url)
{
    var w = 440;
    var h = 400;
    var topPoint = 100;
    var leftPoint = 100;  
    var winFeatureGlossary = window.open(url, 'detail2', 'toolbar=no,location=no,directories=no,status=no,menubar=0,scrollbars=yes,left=' + leftPoint + ',top=' + topPoint + ',resizable=yes,width=' + w + ',height=' + h + '')
	
	if (winFeatureGlossary != null)
	{
		if (winFeatureGlossary.opener == null)
			winFeatureGlossary.opener = self;
		else
			winFeatureGlossary.focus();
	}
}//end of PC_ShowFeatureGlossaryWindow
/*=========================================================================================
/*	end of product comparison function
/*=========================================================================================*/
