// JavaScript Document

function showLinks(divNo) {
//shows different links in the navigator at the top of the page
  for (var i=0;i<7;i++) {
    if (i != divNo && (i != 2 && i != 5)) {
    document.getElementById("links" + i).style.visibility = "hidden";
    }
  }
  document.getElementById("links" + divNo).style.visibility = "visible";
  timeOutString = "document.getElementById(\"links" + divNo + "\").style.visibility = \"hidden\"";
  setTimeout(timeOutString,8000);
}

function add()
{	
	if(trim(document.form1.txtdistance.value) == "" || (trim(document.form1.txtdistance.value))*1 == 0)
	 {
		 alert("Please enter the distance driven in a year, e.g. 12000");
		 document.form1.txtdistance.focus();
		 return false;
	 }
	 else
	 {
	 	if(IsNumeric(document.form1.txtdistance.value) == false) {
			alert('Please enter a valid distance, e.g. 12000');
			document.form1.txtdistance.focus();			
			return false;
	 	}
	 }
	 
	if(trim(document.form1.txtfuleeconomy.value)=="" || (trim(document.form1.txtfuleeconomy.value))*1==0) 
	 {
		 alert("Please enter vehicle fuel economy, e.g. 38.2");
		 document.form1.txtfuleeconomy.focus();
		 return false;
	 }
	 else
	 {
	 	if(IsNumeric(document.form1.txtfuleeconomy.value) == false) {
			alert('Please enter vehicle fuel economy, e.g. 38.2');
			document.form1.txtfuleeconomy.focus();			
			return false;
	 	}
	 }
	 return true; 	 
}
 
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");	
}

function trim( value ) {
	return LTrim(RTrim(value));
	
}

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         	blnResult = false;
         }
      }
   return blnResult;
   }
    

function isEMailAddr(elem) {
	var str = elem;
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
   
	if (!str.match(re)) {
        alert("Please enter the valid email address!");
        return false;
    } else {
        return true;
    }
}

function submitform(page)
{
  if(chk())
  {
	document.cartForm.submit();
  }
  return false;
}


function chk()
{
	var j=0;
	var k=0;
	for(var g=0;g<document.cartForm.elements.length;g++)
	{	
		if(document.cartForm.elements[g].type=="text")
		{
			
			var qty="qty"+j;				
			if(document.getElementById(qty))
			{
				var ObjTarget=document.getElementById(qty).value;			
				if(trim(ObjTarget)=="" || trim(ObjTarget)*1==0) 
				{
					 alert("Please Enter quantity!");				 
					 return false;
				 }
				 else
				 {
				 	if(IsNumeric(ObjTarget) == false) {
						alert('Please enter valid quantity !!');							
						return false;
				 	}
				 }
				 j++;
			}
			var temp="temp"+k;				
			if(document.getElementById(temp))
			{
				var ObjTarget=document.getElementById(temp).value;			
				if(trim(ObjTarget)=="" || trim(ObjTarget)*1==0) 
				{
					 alert("Please Enter quantity!");				 
					 return false;
				 }
				 else
				 {
				 	if(IsNumeric(ObjTarget) == false) {
						alert('Please enter valid quantity !!');							
						return false;
				 	}
				 }
				 k++;
			}
		}
	}
	return true;
}


