// JavaScript Document

// functions for site
// trim ($str)
function trim ($str) {
	$str = $str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
	
	return $str;
}

// PageMeasurements($side)
function PageMeasurements($side) {
	$return = 0;
	
	switch ($side) {
		case 'w':
			$return = Math.max(
				Math.max(document.body.scrollWidth, document.documentElement.scrollWidth),
				Math.max(document.body.offsetWidth, document.documentElement.offsetWidth),
				Math.max(document.body.clientWidth, document.documentElement.clientWidth)
			);
			break;
		case 'h':
    		$return = Math.max(
				Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
				Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
				Math.max(document.body.clientHeight, document.documentElement.clientHeight)
			);
			break;
	}
	
	return $return;
}

// getScrollXY()
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

// MsgBox($url,$focus)
function MsgBox($url,$focus) {
	if ($url == "") {
		document.getElementById('dvMsgBox').innerHTML = "&nbsp;";
		document.getElementById('MsgBox').style.visibility="hidden";
	}
	else {
		$wXY = getScrollXY(); $wX = $wXY[0]; $wY = $wXY[1];
		//$wX = (navigator.appName == "Netscape") ? window.pageXOffset : document.body.scrollLeft;
		//$wY = (navigator.appName == "Netscape") ? window.pageYOffset : document.body.scrollTop;
		//alert($wX + ' ' + $wY);
		document.getElementById('MsgBox').style.height = PageMeasurements('h')+'px';
		document.getElementById('dvMsgBox').style.marginTop = (50+$wY) + "px";

		Ajax_Load($url, 'dvMsgBox', 'sml',$focus);
		document.getElementById('MsgBox').style.visibility="visible";
	}
}

// ShowOneHideOne($show,$hide)
function ShowOneHideOne($show,$hide) {
	document.getElementById($show).style.visibility = "visible";
	document.getElementById($show).style.display = "block";
	document.getElementById($hide).style.visibility = "hidden";
	document.getElementById($hide).style.display = "none";
}

// ShowHide($element)
function ShowHide($element) {
	if (document.getElementById($element).style.display == "none") {
		document.getElementById($element).style.display = "block";
	}
	else {
		document.getElementById($element).style.display = "none";
	}
}

function Spinner($e, $add) {
	if ($add) 
		$e.value = parseInt($e.value) + 1;
	else {
		if ($e.value > 1)
			$e.value = parseInt($e.value) - 1;
	}
}

function RotateAdvert($div) {
	Ajax_Load('widget_adverts.php', $div,'','');
	window.setTimeout("RotateAdvert('"+$div+"');",120000)
}

// limitText(limitField, limitCount, limitNum)
function limitText(limitField, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	}
}

// Register Functions
// Register_AddAccount($input, $list)
function Register_AddAccount($input, $list) {
	$obj_acc = document.getElementById($input);
	$obj_acc.value = trim($obj_acc.value);
	
	if ((parseInt($obj_acc.value) > 0) && ($obj_acc.value.length == 8)) {
		$obj_list = document.getElementById($list);
		$add = true;
		
		for ($i=0; $i<$obj_list.options.length; $i++) {
			if ($obj_list.options[$i].value == parseInt($obj_acc.value)) {
				$add = false;
				alert('Account has been added already.');
			}
		}
		
		if ($add) {
			$item = document.createElement('option');
			$obj_list.options.add($item);
			$item.value=($obj_acc.value);
			$item.text=($obj_acc.value);
		}
		
		$obj_acc.value = '';
		$obj_acc.focus();
	}
	else
		alert('Account numbers are 8 digits long.');
}

// Register_RemoveAccount()
function Register_RemoveAccount($list) {
	$obj_accs = document.getElementById($list);
	
	for ($i=0; $i<$obj_accs.options.length; $i++) {
		if ($obj_accs.options[$i].value == $obj_accs.value) {
			if (confirm('Are you sure you want to remove account ' + $obj_accs.value + ' from list?'))
				$obj_accs.options[$i]=null;
		}
	}
}

// Register_Check()
function Register_Check() {
	$error = "";
	
	if ((trim(document.getElementById('r_email').value) == '') 
	|| (trim(document.getElementById('r_email').value) != trim(document.getElementById('r_email_conf').value))) 
		$error += "   - Email address not entered or does not match with confirmation\n";

	if ((trim(document.getElementById('r_password').value) == '') 
	|| (trim(document.getElementById('r_password').value).length < 8) 
	|| (trim(document.getElementById('r_password').value) !== trim(document.getElementById('r_password_conf').value)))
		$error += "   - Password is less than 8 characters or does not match with confirmation.\n";
	
	if (trim(document.getElementById('r_fname').value) == '')
		$error += "   - First Name is required.\n";
	
	if (trim(document.getElementById('r_lname').value) == '')
		$error += "   - Last Name is required.\n";
	
	if (document.getElementById('r_accounts').options.length <= 0)
		$error += "   - One or more account number(s) is required.\n";
	
	if (document.getElementById('r_terms').checked == false)
		$error += "   - You cannot be registered if you do not agree to our terms.\n";
		
	if ($error != "")
		alert("Please correct the following errors:-\n\n" + $error);
	else {
		$list = document.getElementById('r_accounts');
	
		for ($i=0; $i<$list.options.length; $i++) {
			$list.options[$i].selected=true;
		}
	}

	return ($error == '');
}


// Account Functions
// Account_PasswordChange()
function Account_PasswordChange() {
	$errors = "";
	
	if (trim(document.getElementById('ma_password').value) == "")
		$errors += "   - Current Password not entered.\n";

	if (trim(document.getElementById('ma_newpassword').value).length < 8)
		$errors += "   - New Password cannot be empty or less than 8 characters long.\n";

	if (trim(document.getElementById('ma_newpassword').value) != trim(document.getElementById('ma_newpasswordconf').value))
		$errors += "   - New Password does not match new password confirmation.\n";

	if ($errors != "")
		alert("Following errors were encountered:-\n" + $errors);
		
	return ($errors == "")
}


// Product Functions
// Product_Lookup()
function Product_Lookup() {
	$querystring = "";
	$errors = "";
	
	if (document.getElementById('l_account').value == "")
		$errors += "   - Account not selected.\n";
	
	if (trim(document.getElementById('l_search').value) == "")
		$errors += "   - Search keyword not specified.\n";
	
//	if (trim(document.getElementById('l_markup').value) != "") {
//		if (!parseInt(trim(document.getElementById('l_markup').value)) > 0)
//			$errors += "   - Markup should be a number.\n";
//	}
	
	if ($errors != "")
		alert("The following errors were encountered:\n\n" + $errors);
	else {
		Ajax_Load("compo_lookup_products.php?l_account="+document.getElementById('l_account').value+"&l_search="+document.getElementById('l_search').value+"&l_filter="+document.getElementById('l_filter').value+"&l_type="+document.getElementById('l_type').value+"&l_past="+(document.getElementById('l_past').checked ? 1 : 0)+"&l_prices="+(document.getElementById('l_prices').checked ? 1 : 0)+"&l_sort="+document.getElementById('l_sort').value+"&l_sortorder="+document.getElementById('l_sortorder').value+"&l_markup=",'dvSearch','sml',"");
	}
	
	return false;
}


// Template Functions
// Template_Lookup()
function Template_Lookup() {
	$querystring = "";
	$errors = "";
	
	if (document.getElementById('l_account').value == "")
		$errors += "   - Account not selected.\n";
	
	if (trim(document.getElementById('l_search').value) == "")
		$errors += "   - Search keyword not specified.\n";
	
	if ($errors != "")
		alert("The following errors were encountered:\n\n" + $errors);
	else {
		Ajax_Load("compo_my-account_template_search.php?l_account="+document.getElementById('l_account').value+"&l_search="+document.getElementById('l_search').value+"&l_filter="+document.getElementById('l_filter').value+"&l_type="+document.getElementById('l_type').value+"&l_past="+(document.getElementById('l_past').checked ? 1 : 0)+"&l_sort="+document.getElementById('l_sort').value+"&l_sortorder="+document.getElementById('l_sortorder').value+"",'dvSearch','sml');
	}
	
	return false;
}


// Order Functions

// Order_Submit()
function Order_Submit() {
	if (confirm("Are you sure you want to submit this order?")) {
		document.getElementById('o_do').value = "submit";
		document.fOrder.submit();
	}
}

// Order_Delete()
function Order_Delete() {
	if (confirm("Are you sure you want to delete this order?")) {
		document.getElementById('o_do').value = "delete";
		document.fOrder.submit();
	}
}

// Order_Lookup()
function Order_Lookup() {
	$querystring = "";
	$errors = "";
	
	if (trim(document.getElementById('o_search').value) == "")
		$errors += "   - Search keyword not specified.\n";
	
	if (document.getElementById('o_template').value == 'template')
		$errors = "";
		
	if ($errors != "")
		alert("The following errors were encountered:\n\n" + $errors);
	else {
		Ajax_Load("compo_order_search_products.php?order="+document.getElementById('order').value+"&o_search="+document.getElementById('o_search').value+"&o_filter="+document.getElementById('o_filter').value+"&o_type="+document.getElementById('o_type').value+"&o_past="+(document.getElementById('o_past').checked ? 1 : 0)+"&o_prices="+(document.getElementById('o_prices').checked ? 1 : 0)+"&o_sort="+document.getElementById('o_sort').value+"&o_sortorder="+document.getElementById('o_sortorder').value+"&o_template="+document.getElementById('o_template').value+"",'dvSearch','sml',"");
	}
	
	return false;
}

// Order_AddProduct($order,$product,$quantity)
function Order_AddProduct($order,$product,$quantity) {
	var t = document.getElementById('o_price');
	var ack_short = 0;
	var ack_ret = 0;
	
	if ($quantity == "") {
		alert("Please enter a quantity.");
		return false;
	}
	
	if (document.getElementById('o_returnable')) {
		if (!document.getElementById('o_returnable').checked) {
			alert("Non-returnable items need to be acknowledged before you can order them.");
			return false;
		}
		else 
			ack_ret = 1;
	}
	if (document.getElementById('o_shortdated')) {
		if (!document.getElementById('o_shortdated').checked) {
			alert("Short dated items need acknoledgement before you can order them.");
			return false;
		}
		else 
			ack_short = 1;
	}
	
	if (document.getElementById('dvOrdProducts')) {
		Ajax_Load("compo_order_products.php?order="+$order+"&product["+$product+"]="+$quantity+"&price["+$product+"]="+
			((t != null) ? document.getElementById('o_price').value : "") + "&return_acknowledge="+ack_ret+
			"&short_acknowledge="+ack_short, 'dvOrdProducts','sml',"");

	}
	else {
		Ajax_Load("widget_order_cart.php?order="+$order+"&product["+$product+"]="+$quantity+"&price["+$product+"]="+
			((t != null) ? document.getElementById('o_price').value : "") + "&return_acknowledge="+ack_ret+
			"&short_acknowledge="+ack_short, 'dvCart','sml',"o_search");
	}
	MsgBox('');
	
	if (document.getElementById('rwProd'+$product)) {
        document.getElementById('tblSrchProds').deleteRow(document.getElementById('rwProd'+$product).rowIndex);
	}
	
	if (document.getElementById('o_search')) {
		document.getElementById('o_search').value = '';
		document.getElementById('o_filter').value = '';
		document.getElementById('o_search').focus();
	}
}

// Order_UpdateQuantities($order)
function Order_UpdateQuantities($order) {
	$products = "";
	
	for ($i=0;$i<document.getElementsByName('op_qty').length;$i++) {
		$products += (($products == "") ? "" : "&") + "product["+document.getElementsByName('op_prod')[$i].value+"]=" + 
			document.getElementsByName('op_qty')[$i].value;
	}
	//document.getElementById('o_filter').value = "compo_order_products.php?order="+$order+"&"+$products;
	Ajax_Load("compo_order_products.php?order="+$order+"&"+$products, 'dvOrdProducts','sml',"");
}

// Order_DeleteProducts($order)
function Order_DeleteProducts($order) {
	$products = "";
	
	if (confirm("Are you sure you want to delete selected products from your order?")) {
		for ($i=0;$i<document.getElementsByName('op_tick').length;$i++) {
			if (document.getElementsByName('op_tick')[$i].checked) {
				document.getElementsByName('op_qty')[$i].value = 0;
				
				$products += (($products == "") ? "" : "&") + "product["+document.getElementsByName('op_prod')[$i].value+"]=" + 
					document.getElementsByName('op_qty')[$i].value;
			}
		}
		//document.getElementById('o_filter').value = "compo_order_products.php?order="+$order+"&"+$products;
		if ($products != "")
			Ajax_Load("compo_order_products.php?order="+$order+"&"+$products, 'dvOrdProducts','sml',"");
	}
}

// Order_Quantities($element, e)
function Order_Quantities($element, e) {
	var charCode;
    
    if(e && e.which) {
        charCode = e.which;
		e.which = 9;
    }
	else if(window.event) {
        e = window.event;
        charCode = e.keyCode;
		e.keyCode = 9;
    }

    if(charCode == 13) {
		//$element.form[(getIndex($element)+1) % $element.form.length].focus();
        alert("Enter was pressed");
    }
}

  
// Returns
// Return_Delete()
function Return_Delete() {
	if (confirm("Are you sure you want to delete this return?")) {
		document.getElementById('r_do').value = "delete";
		document.fReturn.submit();
	}
}

// Return_Submit()
function Return_Submit() {
	if (confirm("Are you sure you want to submit this return?")) {
		document.getElementById('r_do').value = "submit";
		document.fReturn.submit();
	}
}

// Return_Invoice_Products($return, $acc, $inv, $date)
function Return_Invoice_Products($return, $acc, $inv, $date) {
	Ajax_Load('compo_return_inv_products.php?return='+$return+'&account='+$acc+'&invoice='+$inv+'&date='+$date, 'dvProducts','sml','');
}

// Return_AddToReturn($return,$invoice,$date,$product)
function Return_AddToReturn($return,$invoice,$date,$product) {
	if (document.getElementById('r_reason').value == "") {
		alert("Please select a reason for returning product."); return false;
	}
	if (document.getElementById('r_qty').value == "") {
		alert("Please enter a quantity to return."); return false;
	}
	else {
		if (parseInt(document.getElementById('r_qty').value) <= 0) {
			alert("Please enter a quantity to return."); return false;
		}
	}
	
	Ajax_Load('compo_return_products.php?return='+$return+'&invoice='+$invoice+'&date='+$date+'&product='+$product+'&reason='+document.getElementById('r_reason').value+'&quantity='+document.getElementById('r_qty').value+'&comments='+document.getElementById('ret_comments').value, 'dvRetProducts','sml','');
	MsgBox('','');
}

// Return_RemoveFromReturn($return,$invoice,$date,$product)
function Return_RemoveFromReturn($return,$invoice,$date,$product) {
	Ajax_Load('compo_return_products.php?return='+$return+'&invoice='+$invoice+'&date='+$date+'&product='+$product+'&do=delete', 'dvRetProducts','sml','');
	MsgBox('','');
}

// 
function Sales_Report() {
	$accounts = "";
	
	for ($i = 0; $i < document.getElementsByName('sd_account').length; $i++) {
		if (document.getElementsByName('sd_account')[$i].checked)
			$accounts += (($accounts == "") ? "" : "&") + "sdaccount[]=" + document.getElementsByName('sd_account')[$i].value;
	}
	
	if ($accounts == "")
		alert("Please select some accounts first.");
	else
		window.open("salesdata_report.php?"+$accounts);
		
	return false;
}

