/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var ajax = new sack();
function findPos(obj)
{
	var curleft = curtop = 0;

	if (obj.offsetParent)
	{
		curleft = obj.offsetLeft

		curtop = obj.offsetTop

		while (obj = obj.offsetParent)
		{
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
function showWait()
{
	var oAnchor = document.getElementById('td_wait');
	var pos = findPos(oAnchor);
	var oDiv = document.getElementById('pleaseWait');
	oDiv.innerHTML = '<table><tr><td>Please wait. The code is being validated... &nbsp;</td><td>&nbsp;<img src="/images/ajax.gif" /></td></tr></table>';
	oDiv.style.visibility = 'visible';
	oDiv.style.left = pos[0] + 250 +'px';
	oDiv.style.top = pos[1] - 0 + oAnchor.offsetHeight +'px';
	oDiv.style.zIndex = 3;
}
function showLoading()
{
	if(document.getElementById('prepage')!=null)
    {
        document.getElementById('prepage').style.display='';
    }
}
function hideWait()
{
    if(document.getElementById('pleaseWait')!=null)
    {
        document.getElementById('pleaseWait').innerHTML='';
        document.getElementById('pleaseWait').style.visibility='hidden';
    }
	if(document.getElementById('prepage')!=null)
    {
        document.getElementById('prepage').style.display='none';
    }
}

function getVolumeDiscountInformation(uid)
{
	if(document.getElementById('coupon_threshold')!=null)
	{
		var threshold = document.getElementById('coupon_threshold').value;
		var shipping_cost = document.getElementById('shipping_cost').value;
		var total_price = document.getElementById('total_price').value;
		var total_salestax = document.getElementById('sales_tax_amount').value;
		if(total_salestax == '')
			total_salestax = 0;
		ajax.requestFile='ajax-server.php?volume_discount=yes&user_id='+uid+'&threshold='+threshold+'&shipping_cost='+shipping_cost+'&total_price='+total_price+'&total_salestax='+total_salestax;
		ajax.onCompletion = parseResponse;
		ajax.runAJAX();
	}
}

function checkCoupon()
{
	if(document.getElementById('discount_done'))
	{
		var discount_done = document.getElementById('discount_done').value;
		var code = document.getElementById('coupon_code').value;
		var threshold = document.getElementById('coupon_threshold').value;
		var shipping_cost = document.getElementById('shipping_cost').value;
		var total_price = document.getElementById('total_price').value;
		var total_salestax = document.getElementById('sales_tax_amount').value;
		var member_discount = document.getElementById('member_discount_value').value;
		if(total_salestax == '')
			total_salestax = 0;
		
		if(code!='')
		{
			if(discount_done == 1)
			{
				alert('You have already availed coupon discount.\n Multiple coupon discount is not permisible for same order.');
				return false;
			}
			showWait();
			if(code.substr(0,2).toUpperCase() == 'IA')
			{
				ajax.requestFile='ajax-server.php?agent_code='+code+'&threshold='+threshold+'&shipping_cost='+shipping_cost+'&total_price='+total_price+'&member_discount='+member_discount+'&total_salestax='+total_salestax;
			}
			else
			{
				ajax.requestFile='ajax-server.php?coupon_code='+code+'&threshold='+threshold+'&shipping_cost='+shipping_cost+'&total_price='+total_price+'&member_discount='+member_discount+'&total_salestax='+total_salestax;
			}
			//alert(ajax.requestFile);
			ajax.onCompletion = parseResponse;
			ajax.runAJAX();
		}
		else
		{
			//alert("If you have a coupon code, please enter it.");
			document.getElementById('coupon_code').focus();
			return false;
		}
	}
	else
		return false;
}

function submitenter_coupon(myfield,e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	
	if (keycode == 13)
	{
	   isDiscountDone();
	}
	else
	   return true;
}

function isDiscountDone()
{
	if(document.getElementById('discount_done'))
	{
		var discount_done = document.getElementById('discount_done').value;
    
		if(discount_done==1)
		{
			alert('You have already availed coupon discount.\n Multiple coupon discount is not permisible for same order.');
			return true;
		}
		else
		{
			checkCoupon();
			return false;
		}
	}
	else
		return false;
}


function authenticateAdmin(id,val,type)
{
    ajax.requestFile='ajax-server.php?account_userId='+id+'&pay_type='+type+'&pwd='+val;
    ajax.onCompletion = parseResponse;
    ajax.runAJAX();
}

function authenticateAdminConfirm(id,val,type)
{
    ajax.requestFile='ajax-server.php?account_userId='+id+'&pay_type='+type+'&pwd='+val+'&confirm=yes';
    ajax.onCompletion = parseResponse;
    ajax.runAJAX();
}

function getStyleDetails(style_id)
{
	//showLoading();
	document.getElementById('style_'+style_id).checked = true;
	var lf = document.getElementById('txtLinearFeet').value;
	document.getElementById('kit_image').innerHTML='<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p align="center">Loading kitchen image and details...<img src="/images/loading.gif" title="Loading kitchen image and details..." alt="Loading kitchen image and details..." /></p>';
	ajax.requestFile='/ajax/ajax-server.php?stock_style_id='+style_id+'&linear_feet='+lf;
    ajax.onCompletion = parseResponse;
    ajax.runAJAX();
}

function getCabinetsEstimate()
{
	//showLoading();
	var style_id = document.getElementById('txtStyleId').value;
	var lf = document.getElementById('txtLinearFeet');
	if(style_id == '')
	{
		alert("Select the cabinet style you're interested in.");
		return false;
	}
	else if(isNaN(lf.value) || lf.value == '' || lf.value <= 0)
	{
		alert("Please enter numeric value > 0 for your kitchen linear feet.");
		lf.focus();
		if(document.getElementById('div_estimate_price').style.display =='')
			document.getElementById('div_estimate_price').style.display = 'none';
		return false;
	}
	else
	{
		
		document.getElementById('div_estimate_price').style.display ='';
		document.getElementById('estimated_price').innerHTML = '<img src="/images/loading.gif" title="Calculating..." alt="Calculating..." />';
		ajax.requestFile='/ajax/ajax-server.php?lf='+lf.value+'&style_id='+style_id;
		ajax.onCompletion = parseResponse;
		ajax.runAJAX();
	}
}

function parseResponse()
{
	//alert(ajax.response);
	eval(ajax.response);
    hideWait();
}

