// JavaScript Document

/********************************
** CLEAR CERTAIN FAQ DIV BOXES **
********************************/
function clearFAQDiv(){
	document.getElementById('submit').disabled = true;
	document.getElementById('faqQ').innerHTML = '<img src="../images/loading.gif" /> Please wait while we search...';	
	document.getElementById('faqA').innerHTML = '';	
	document.getElementById('faqR').innerHTML = '';
}

/*************************
** ENABLE BUTTON STATUS **
*************************/
function buttonstatus(stat, id){
	if(stat != 'on')
	document.getElementById(id).disabled = true;
	else
	document.getElementById(id).disabled = false;
	}

/************************************************
** FORCE INPUT TO BE JUST ALPHABETS OR NUMBERS **
************************************************/
function alphanumeric(id){
	var input = document.getElementById(id).value
	
	// clear if starts with a space
	if (Left(input, 1) == ' ') document.getElementById(id).value = ''
	
	// truncate if over the limit
	if(input.length > 1024) document.getElementById(id).value = Left(input, 1024);
	
}

/***********************
** PASSWORD GENERATOR **
***********************/
function generatePassword(id, length){
  chars = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789";
  pass = "";
  for(x=0;x<length;x++)
  {
    i = Math.floor(Math.random() * chars.length);
    pass += chars.charAt(i);
  }
  document.getElementById(id).value = pass;
}

/********************************
** CHECK x CHARS FROM THE LEFT **
********************************/
function Left(str, n){
	if (n <= 0) return "";
	else if (n > String(str).length) return str;
	else return String(str).substring(0,n);
}
/********************************
** CHECK x CHARS FROM THE RIGHT **
********************************/
function Right(str, n){
    if (n <= 0) return "";
    else if (n > String(str).length) return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}	

/*****************************************************************************************************
** BUILT SPECIFICALLY FOR LIST PRICE FOR FIELD LINEARITY MONTHLY AUTO INPUT ACCORDING TO PERCENTAGE **
*****************************************************************************************************/
function linearitymonthly(models){
	var m = models.split(", "); //[m]odel
	var valueamount
	var mp = document.getElementById('mp').value //[m]onthly [p]ercentage
	
	for(v in  m){
		valueamount = document.getElementById(m[v] + 'valueamount').value
		
			//if the cell is empty, don't change the value
			if(document.getElementById(m[v] + '10').value.length > 0) 
				document.getElementById(m[v] + '10').value = (mp / 100) * valueamount;
		}

	}
	
/*************************************************************************************
** BUILT SPECIFICALLY FOR LIST PRICE'S POS REBATE TO CLEAR A WHOLE COLUMN OF FIELDS **
*************************************************************************************/	
function clearPosRebateContents(models){
	var m = models.split(", "); //[m]odel
	
	for(v in m){
		document.getElementById(m[v]).value = ''
		}
	}	

/****************************************
** NUMBER FORMAT ACCORDING TO CURRENCY **
****************************************/
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
//return (((sign)?'':'-') + '$' + num + '.' + cents);
return (((sign)?'':'-') + num + '.' + cents);
}

/*****************************************************
** RETURNS TRUE OR FALSE IF VALUE IS NUMERIC OR NOT **
*****************************************************/
function IsNumeric(sText){
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) IsNumber = false;
      }
   return IsNumber;
}

/************************
** ALLOWS NUMBERS ONLY **
************************/
function NumericOnly(id){
	var v = document.getElementById(id).value
	var ValidChars = "0123456789";
	var Char;
	
	for(i = 0; i < v.length; i++){
		Char = v.charAt(i);
		if(ValidChars.indexOf(Char) == -1){
		document.getElementById(id).value = v.replace(/[^0-9]/g, '');
		}
		}
	
	}

/*************************************
** ALLOWS NUMBERS AND DECIMALS ONLY **
*************************************/
function NumericDecimal(id){
	var v = document.getElementById(id).value
	var ValidChars = "0123456789.";
	var Char;
	
	for(i = 0; i < v.length; i++){
		Char = v.charAt(i);
		if(ValidChars.indexOf(Char) == -1) document.getElementById(id).value = Left(v, v.length - 1);
		}
	
	}

/********************************************************
** BUILD FOR MLINK NAVIGATION DROP DOWN AUTO JUMP MENU **
********************************************************/
function land(ref, target)
{
lowtarget=target.toLowerCase();
if (lowtarget=="_self") {window.location=loc;}
else {if (lowtarget=="_top") {top.location=loc;}
else {if (lowtarget=="_blank") {window.open(loc);}
else {if (lowtarget=="_parent") {parent.location=loc;}
else {parent.frames[target].location=loc;};
}}}
}
function jump(menu)
{
ref=menu.choice.options[menu.choice.selectedIndex].value;
splitc=ref.lastIndexOf("*");
target="";
if (splitc!=-1)
{loc=ref.substring(0,splitc);
target=ref.substring(splitc+1,1000);}
else {loc=ref; target="_self";};
if (ref != "") {land(loc,target);}
}


/************************
** AJAX DROP DOWN MENU **
************************/
var arrayItems;
function check(evt)
{
	var e = new MouseEvent(evt);
	var target = null;
	var img;
	var menuDiv = new Array("menuhomediv", "menuproductsdiv", "menulogindiv", "menuwarrantydiv", "menucontactdiv", "menunewsdiv", "menufaqdiv", "menuaboutusdiv");
	var menuImg = new Array("menuhome", "menuproducts", "menulogin", "menuwarranty", "menucontact", "menunews", "menufaq", "menuaboutus");
		
//
//  getData
//	
	for(var menuid in menuImg){
		img = document.getElementById(menuImg[menuid]);
		if(
		e.x > parseInt(img.style.left) && 
		e.y > parseInt(img.style.top) &&
		e.x < (parseInt(img.style.left) + parseInt(img.style.width)) && 
		e.y < (parseInt(img.style.top) + parseInt (img.style.height))
		){
		getData(img.id);
		}
	}
	
//
//  Hide Menu Divs
//	
	for(var menuname in menuDiv){
		target = document.getElementById(menuDiv[menuname]);
		img = document.getElementById(menuImg[menuname]);
		if (target.style.visibility == "visible"){
			if(
			   e.x < parseInt(target.style.left) || 
			   e.y < parseInt(img.style.top) ||
			   e.x > (parseInt(img.style.left) + parseInt(img.style.width))	|| 
			   e.y > (parseInt(target.style.top) + parseInt(target.style.height))
			   ){
				hide(menuDiv[menuname]);
				}
			}
		}
	}

function MouseEvent(e){
	if(e) {
	this.e = e;
	} else {
	this.e = window.event;
	}
	
	if(e.pageX) {
	this.x = e.pageX;
	} else {
	this.x = e.clientX;
	}
	
	if(e.pageY) {
	this.y = e.pageY;
	} else {
	this.y = e.clientY;
	}
	
	if(e.target) {
	this.target = e.target;
	} else {
	this.target = e.srcElement;
	}
}

function hide(target)
{	
	if(document.getElementById(target)){
		var menuDiv1 = null;
		menuDiv1 = document.getElementById(target);
		if(menuDiv1.style.visibility == "visible"){
			menuDiv1.innerHTML = "<div></div>";
			menuDiv1.style.visibility = "hidden";
		}
	}
}

function getData(menu)
{
var XMLHttpRequestObject = false;
if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
XMLHttpRequestObject = new
ActiveXObject("Microsoft.XMLHTTP");
}

var dataSource = "/script/"+menu+".txt";
if(XMLHttpRequestObject) {
XMLHttpRequestObject.open("GET", dataSource);
XMLHttpRequestObject.onreadystatechange = function()
{
if (XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200) {
	show(menu, XMLHttpRequestObject.responseText);
	}
}
XMLHttpRequestObject.send(null);
}
}

function show(menu, items)
{
var data = "<table width='100%' class='dropdownmenu'>";
var loopIndex;
arrayItems = items.split(", ");
var target;

if (arrayItems.length != 0) {
for (var loopIndex = 0; loopIndex < arrayItems.length;
loopIndex++) {
data += "<tr><td>" +
arrayItems[loopIndex] +
"</td></tr>";
}
}

data += "</table>";
	target = document.getElementById(menu+"div");
	if(target.style.visibility == "hidden"){
	target.innerHTML = data;
	target.style.visibility = "visible";
	}
}

/***********************
** BRING MSG TO LIVE! **
***********************/
function highlight(divID){
	if(document.getElementById(divID) != null){
	obj = document.getElementById(divID);
	obj.style.background = "#FFFF00";
	setTimeout(dimmer, 3456);
	}
}

function dimmer()
{
var obj = document.getElementById("msg");
obj.style.color = "#CC6600";
obj.style.background = "#FFFFFF";
}