

function autoAlertCheck(theForm){
	//Email
	var email = theForm.email.value;
	var name = theForm.name.value;
		if(name.length == 0)
	{
		   alert("Please enter Name");
		   theForm.name.focus();
		   return false;
	}
	if(email.length == 0)
	{
		   alert("Please enter Email ID");
		   theForm.email.focus();
		   return false;
	}
	if(isValidEmail(email) == 0)
	{	   
		   theForm.email.focus();
		   return false;		  
	}
	return true;
}

function resetAdvSearch(theForm)
{
	document.advanceSearchForm.type_id.value = "";
	document.advanceSearchForm.subtype_id.value = "";
	document.advanceSearchForm.make_id.value = "";
	document.advanceSearchForm.maker_id.value = "";
	document.advanceSearchForm.auction_house_id.value = "";
	document.advanceSearchForm.model_id.value = "";
	document.advanceSearchForm.mfg_year.value = "";
	document.advanceSearchForm.fuel.value = "";
	document.advanceSearchForm.color.value = "";
	document.advanceSearchForm.transmission.value = "";
	document.advanceSearchForm.engine_capacity.value = "";
	return false;
}

function checkTopSearch(theForm)
{
	var str = theForm.key.value;
	str = trim(str);

	if(str.length == 0)
	{
	     alert("Please enter keywords to search !");
		 theForm.key.value="";
		 theForm.key.focus();
		 return false;
	}
	else if(str.length <3)
	{
	     alert("Please enter atleast 3 characters to search !");
		 theForm.key.focus();
		 return false;	
	}

	return true;
}

function disableRightClick(e)
{
  var message = "Copyright 01 TRCUK.COM";
  
  if(!document.rightClickDisabled) // initialize
  {
    if(document.layers) 
    {
      document.captureEvents(Event.MOUSEDOWN);
      document.onmousedown = disableRightClick;
    }
    else document.oncontextmenu = disableRightClick;
    return document.rightClickDisabled = true;
  }
  if(document.layers || (document.getElementById && !document.all))
  {
    if (e.which==2||e.which==3)
    {
      alert(message);
      return false;
    }
  }
  else
  {
    alert(message);
    return false;
  }
}
//disableRightClick();

function MM_popupMsg(msg) { //v1.0
  alert(msg);
}


function submitForm(theForm)
{
	  theForm.submit();
}
function nullifyControl(theForm)
{
	theForm.loginid.value = "";
	theForm.password.value = "";
}
function isNotID(str){
	for (var i = 0; i < str.length; i++)
	{
			var ch = str.substring(i, i + 1);
			if((ch < '0' || '9' < ch) && ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch))) 
			{
				if(ch == "_") continue;
				return true;
			}
	}
	return false;
}
function textLimitCheck(theForm,theControl,maxLength)
{
	if (theForm.value.length > maxLength)
	{
		alert(maxLength + ' characters limit. \r Excessive data will be truncated.');
		theForm.value = theForm.value.substring(0, maxLength-1);
		theForm.focus();
	}
	theControl.value = theForm.value.length;
}
function checkAll(checked)
{
	  for(var i=0;i<document.form.elements.length;i++)
	  {  
		 	var e = document.form.elements[i];
			if(e.type == "checkbox") e.checked = checked;
	  }
	  
}
function trim (strVar) { 
     if(strVar.length >0)
	 {
	        while(strVar.charAt(0)==" ")  //Left Trim
			strVar=strVar.substring(1,strVar.length); 
			while(strVar.charAt(strVar.length-1)==" ")  //Right Trim
			strVar=strVar.substring(0,strVar.length-1); 			
	 } return strVar; 
}

function bidNow()
{
    var checkedNum = getCheckedNum();
    var cartItemMax = parseInt(document.form.cartItemMax.value);
    var cartItemNum = parseInt(document.form.cartItemNum.value); 
    var urlString = document.location + ""; 
    
    if(checkedNum == 0) {
		if(cartItemNum > 0)
		{
			document.form.action="auction_cart.php";
			document.form.target="";//_blank
			document.form.submit();
		}
		else
		{
			alert("Please check the items to Bid !");
		}
    }

    if(checkedNum > 0) {
		if(cartItemMax <= cartItemNum) {
        	alert("Your Auction Cart is full. It can store a maximum of "+cartItemMax+" auction items.");
        	return;
    	}	
		if(cartItemMax < (cartItemNum+checkedNum)) {
        	alert("It will exceed the maximum limit of auction items ("+cartItemMax+"). At present you have "+cartItemNum+" auction items.");
        	return;
    	}
		document.form.action="auction_cart.php";     
        document.form.target="";
        document.form.submit();
    }
}

function auctionOrder()
{
	var num = 0;  
	var isSubmit = 0;//false
	
    for(var i=0;i<document.form.elements.length;i++)
    {  
		var e = document.form.elements[i];

		if (e.type == "text")
		{
           	var price = e.value;
			if(price >0) isSubmit = 1; //true
			
            num++;
        }
    }	    
	if(isSubmit == 1)
	{
		document.form.action="auction_order_bid.php";     
		document.form.target="";
		document.form.submit();
	}
	else alert("Please enter your Bid Price for the vehicle(s) you want to place BID !");
}

function auctionQuotation()
{
	var num = 0;  
	var isSubmit = 0;//false
	
    for(var i=0;i<document.form.elements.length;i++)
    {  
		var e = document.form.elements[i];

		if (e.type == "text")
		{
           	var price = e.value;
			if(price >0) isSubmit = 1; //true
			
            num++;
        }
    }	    
	if(isSubmit == 1)
	{
	
		document.form.action="auction_quotation_back.php";     
		document.form.target="";
		document.form.submit();
	}
	else alert("Please enter your Bid Price for the vehicle(s) you want to place QUOTATION !");
	
	
}

function cartToBid()
{
	var cartItemMax = parseInt(document.form.cartItemMax.value);
    var cartItemNum = parseInt(document.form.cartItemNum.value);
	if(cartItemMax <= cartItemNum) {
        alert("Your Auction Cart is full. It can store a maximum of "+cartItemMax+" auction items.");
        return;
    }
	document.form.action="auction_cart.php";     
	document.form.target="";
	document.form.submit();
}

function addToCart()
{
    var cartItemMax = parseInt(document.form.cartItemMax.value);
    var cartItemNum = parseInt(document.form.cartItemNum.value);
    var num = getCheckedNum();
	
	if(cartItemMax <= cartItemNum) {
        alert("Your Auction Cart is full. It can store a maximum of "+cartItemMax+" auction items.");
        return;
    }	
	if(cartItemMax < (cartItemNum+num)) {
        alert("It will exceed the maximum limit of auction items ("+cartItemMax+"). At present you have "+cartItemNum+" auction items.");
        return;
    }
	if(num == 0) {
        alert("Please select at least one item to add.");
        return;
    }	
	
    document.form.action=window.location.href;
    document.form.target="";
    document.form.submit();
}
function compareSubmit()
{
    var num = getCheckedNum();
    if(num == 0) {
    	alert("Sorry, please select one or more documents.");
		return;
    }
    var productNum = getProductCheckedNum();
	var urlString = document.location+"";
	if(urlString.indexOf("offer") >= 0){
		var buyOfferNum = getCheckedItemNum('chkBuyOfferIds');
		if(buyOfferNum > 4) {
	    	alert("Sorry, at most 4 buy trade leads you can chose to compare!");
			return;
    	}
    	var sellOfferNum = getCheckedItemNum('chkSellOfferIds');
		if(sellOfferNum > 4) {
    	alert("Sorry, at most 4 sell trade leads you can chose to compare!");
		return;
    	}
		if(buyOfferNum > 0){
		  	//document.form.action = script_web_server;
		  	//document.form.submit(); 
	
		}
		if(sellOfferNum>0){
	    	//document.form.action = script_web_server;
		    //document.form.submit(); 
	    }
	}

    if(urlString.indexOf("sample") >= 0){
    	var productNum = getCheckedItemNum('product_id');
    	if(productNum > 4) {
	    	alert("Sorry, at most 4 products you can chose to compare!");
			return;
   		 }
   		//document.form.action = script_web_server;
	    //document.form.submit(); 
    }

    if(urlString.indexOf("company") >= 0){
		var companyNum = getCheckedItemNum('product_id');
		if(companyNum > 4) {
	    	alert("Sorry, at most 4 Company you can chose to compare!");
			return;
   		}
		//document.form.action = script_web_server;
   		//document.form.submit(); 		
	}
}
function isNotInt(str){
	for (var i = 0; i < str.length; i++)
	{
		var ch = str.substring(i, i + 1);
		if(ch < '0' || '9' < ch) 
		{					
			return true;
		}
	}
	return false;
}
function checkPrice(theForm,startPrice)
{
	 var bidPrice = theForm.value;
	 if(bidPrice >0 && bidPrice<= startPrice)
	 {
		  alert("Bid price must be greater than Start Price ("+ startPrice + ")!");
		  theForm.focus();
	 }
}
function check(checked,theForm,id)
{
	var bidPrice = theForm.value;
	if(bidPrice.length == 0)
	{
		checked = false;
	}
	if(isNotInt(bidPrice))
	{
		alert("Please input only numeric characters ex. 500000");
		theForm.focus();		
	}
	else
	{		
		for(var i=0;i<document.form.elements.length;i++)
		{  
			var e = document.form.elements[i];
			if(e.type == "checkbox") 
			{
				//if(e.value == id)
				e.checked = checked;
			}
		}
	}
}
function onDeleteSubmit(item)
{
    var num = getCheckedNum();	
    if(num == 0) {
    	alert("Sorry, please select one or more documents.");
    	return;
    }
    if(item==1){
    	var buyOfferNum = getCheckedItemNum('chkBuyOfferIds');
	   	document.basket_form.action = "http://my.alibaba.com/apps/feedback?req_page=basket.init&&req_cmd=delbasket&&type=buyOffer"
	    document.basket_form.submit(); 
    }
    if(item==2){
    	var sellOfferNum = getCheckedItemNum('chkSellOfferIds');
    	document.basket_form.action = "http://my.alibaba.com/apps/feedback?req_page=basket.init&&req_cmd=delbasket&&type=sellOffer"
	    document.basket_form.submit(); 
    }
    if(item==3){
    	var productNum = getCheckedItemNum('chkProductIds');
   		document.basket_form.action = "http://my.alibaba.com/apps/feedback?req_page=basket.init&&req_cmd=delbasket&&type=product"
	    document.basket_form.submit(); 
    }
    if(item==4){
		var companyNum = getCheckedItemNum('chkCompanyIds');
		document.basket_form.action = "http://my.alibaba.com/apps/feedback?req_page=basket.init&&req_cmd=delbasket&&type=company"
   		document.basket_form.submit(); 		
	}
}
function auctionSubmit()
{
    var num = getCheckedNum();
    if(num == 0) {
    	alert("No auction items selected! Please select first.");
		return false;
    }
    document.form.action ="auction_cart.php#1";
    document.form.submit();
}
function currencyChange()
{
    document.form.action ="auction_cart.php";
    document.form.submit();
}
function onCompareSubmit(item)
{
    var num = getCheckedNum();
    if(num == 0) {
    	alert("Sorry, please select one or more documents.");
    	return;
    }
    if(item==1){
    	var buyOfferNum = getCheckedItemNum('chkBuyOfferIds');
        if(buyOfferNum > 4) {
	    	alert("Sorry, at most 4 buy trade leads you can chose to compare!");
			return;
    	}
	   	document.basket_form.action = "http://my.alibaba.com/apps/feedback?req_page=basket.compareItem&&req_cmd=compareItem&&type=buyOffer"
	    document.basket_form.submit(); 
    }
    if(item==2){
    	var sellOfferNum = getCheckedItemNum('chkSellOfferIds');
		if(sellOfferNum > 4) {
    	alert("Sorry, at most 4 sell trade leads you can chose to compare!");
		return;
    	}
    	document.basket_form.action = "http://my.alibaba.com/apps/feedback?req_page=basket.compareItem&&req_cmd=compareItem&&type=sellOffer"
	    document.basket_form.submit(); 
    }
    if(item==3){
    	var productNum = getCheckedItemNum('chkProductIds');
    	if(productNum > 4) {
	    	alert("Sorry, at most 4 products you can chose to compare!");
			return;
   		 }
   		document.basket_form.action = "http://my.alibaba.com/apps/feedback?req_page=basket.compareItem&&req_cmd=compareItem&&type=product"
	    document.basket_form.submit(); 
    }
    if(item==4){
		var companyNum = getCheckedItemNum('chkCompanyIds');
		if(companyNum > 4) {
	    	alert("Sorry, at most 4 Company you can chose to compare!");
			return;
   		}
		document.basket_form.action = "http://my.alibaba.com/apps/feedback?req_page=basket.compareComp&&req_cmd=compareCom&&paraType=inquire"
   		document.basket_form.submit(); 		
	}
}
function getCheckedNum()
{
    var num = 0;                
    for(var i=0;i<document.form.elements.length;i++)
    {  
        var e = document.form.elements[i];
        if(e.type == "checkbox")
		{
                if(e.checked) num++;
		}
    }
    return num;
}
function getBuyOfferCheckedNum()
{
    var num = 0;                
    for(var i=0;i<document.form.elements.length;i++)
    {  
        var e = document.form.elements[i];
        if (e.checked)
		{
           
                num++;
        }
    }
    return num;
}

function getSellOfferCheckedNum()
{
    var num = 0;                
    for(var i=0;i<document.form.elements.length;i++)
    {  
        var e = document.form.elements[i];
        if (e.checked) {
           
                num++;
        }
    }
    return num;
}

function getCompanyCheckedNum()
{
    var num = 0;                
    for(var i=0;i<document.form.elements.length;i++)
    {  
        var e = document.form.elements[i];
        if (e.checked) {
           
                num++;
        }
    }
    return num;
}
function getProductCheckedNum()
{
    var num = 0;                
    for(var i=0;i<document.form.elements.length;i++)
    {  
        var e = document.form.elements[i];
        if (e.checked) {
           
                num++;
        }
    }
    return num;
}

function getCheckedItemNum(item)
{
    var num = 0;                
    for(var i=0;i<document.form.elements.length;i++)
    {  
        var e = document.form.elements[i];
        if (e.name == item && e.checked) {
           
                num++;
        }
    }
    return num;
}

function uncheckQuote()
{
        for (var i=0; i<document.form.elements.length;i++)
		{
	        var e = document.form.elements[i];
	        if (e.checked) {          
	                document.form.elements[i].checked = false;
	        }
        }
}
function uncheckRFQ()
{
	for (var i=0; i<document.form.elements.length;i++){
	        var e = document.form.elements[i];
	        if (e.checked) {      
	                document.form.elements[i].checked = false;
	        }
    }	
	
}
function isEmailAddr(email)
{
	var result = false;
	var theStr = new String(email);
	var index = theStr.indexOf("@");
	if (index > 0)
	{
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1))
		result = true;
	}
	return result;
}

function validRequired(formField,fieldLabel)
{
	var result = true;
	
	if (formField.value == "")
	{
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}
	
	return result;
}

function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
	var result = true;
	
	// Note: doesn't use regular expressions to avoid early Mac browser bugs
	for (var i=0;i<str.length;i++)
	if (charset.indexOf(str.substr(i,1))<0)
	{
		result = false;
		break;
	}
	
	return result;
}

function isValidEmail(emailid){		
		var l=emailid.length;
		if(l==0)
		{
				return false;	
		}
		if(l!=0)
		{
				var a=emailid.indexOf('@');
				var d=emailid.lastIndexOf('.');
				var str1=emailid.substr(0,a);
				var str2=emailid.substr(a+1,d-a-1);
				var str3=emailid.substr(d+1,l);
				var len1=str1.length;
				var len2=str2.length;
				var len3=str3.length;
				if(a<0 || d<2)
				{
						alert ("Check for missing '@' or '.' ");
						return false;
				}
				else if (a>d)
				{
						alert ("Invalid email. Please enter correct email address");
						return false;
				}				
				if (len1<=1 || len2<=1 || len3 <=1)
				{
						alert ("Invalid email. Please enter correct email address");
						return false;
				}				
		}
		return true;
}
function validNum(formField,fieldLabel,required)
{
	var result = true;
	
	if (required && !validRequired(formField,fieldLabel))
	result = false;
	
	if (result)
	{
		if (!allDigits(formField.value))
		{
			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();
			result = false;
		}
	}
	
	return result;
}


function validInt(formField,fieldLabel,required)
{
	var result = true;
	
	if (required && !validRequired(formField,fieldLabel))
	result = false;
	
	if (result)
	{
		var num = parseInt(formField.value,10);
		if (isNaN(num))
		{
			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();
			result = false;
		}
	}
	
	return result;
}


function validDate(formField,fieldLabel,required)
{
	var result = true;
	
	if (required && !validRequired(formField,fieldLabel))
	result = false;
	
	if (result)
	{
		var elems = formField.value.split("/");
		
		result = (elems.length == 3); // should be three components
		
		if (result)
		{
			var month = parseInt(elems[0],10);
			var day = parseInt(elems[1],10);
			var year = parseInt(elems[2],10);
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
			allDigits(elems[1]) && (day > 0) && (day < 32) &&
			allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
		}
		
		if (!result)
		{
			alert('Please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.');
			formField.focus();
		}
	}
	
	return result;
}

function validateFormLead(theForm)
{
	if (!validRequired(theForm.lead_title,"Lead Title"))
	return false;
	if (!validRequired(theForm.expiry_date,"Expiry Date"))
	return false;	
	if (!validRequired(theForm.vehicle_type,"Vehicle Type"))
	return false;	
	if (!validRequired(theForm.make,"Make"))
	return false;	
	if (!validRequired(theForm.model,"Model"))
	return false;	
	if (!validRequired(theForm.mfg_year,"Manufacturing Year"))
	return false;	
	if (!validRequired(theForm.price_min,"Minimum Price"))
	return false;	
	if (!validRequired(theForm.price_max,"Maximum Price"))
	return false;	
	if (!validRequired(theForm.mileage,"Mileage"))
	return false;
	
	return true;
}

function validateFormLogin(theForm)
{
	if (!validRequired(theForm.loginid,"Login ID"))
	return false;	
	if (!validRequired(theForm.password,"Password"))
	return false;
	
	return true;
}

function validateFormRegister(theForm)
{
	if (!validRequired(theForm.loginname,"Login Name"))
	return false;	
	if (!validRequired(theForm.password,"Password"))
	return false;	
	if (!validRequired(theForm.name,"Name"))
	return false;	
	if (!validRequired(theForm.address1,"Address 1"))
	return false;
	if (!validRequired(theForm.city,"City"))
	return false;	
	if (!validRequired(theForm.country,"Country"))
	return false;	
	if (!validRequired(theForm.email1,"E-Mail 1"))
	return false;
	
	return true;
}

function SymError()
{
  return true;
}

function disableRightClick(e)
{
  var message = "Copyright 01 TRCUK.COM";
  
  if(!document.rightClickDisabled) // initialize
  {
    if(document.layers) 
    {
      document.captureEvents(Event.MOUSEDOWN);
      document.onmousedown = disableRightClick;
    }
    else document.oncontextmenu = disableRightClick;
    return document.rightClickDisabled = true;
  }
  if(document.layers || (document.getElementById && !document.all))
  {
    if (e.which==2||e.which==3)
    {
      alert(message);
      return false;
    }
  }
  else
  {
    alert(message);
    return false;
  }
}

function MM_popupMsg(msg) { //v1.0
  alert(msg);
}

function MM_checkPlugin(plgIn, theURL, altURL, autoGo) { //v4.0
  var ok=false; document.MM_returnValue = false;
  with (navigator) if (appName.indexOf('Microsoft')==-1 || (plugins && plugins.length)) {
    ok=(plugins && plugins[plgIn]);
  } else if (appVersion.indexOf('3.1')==-1) { //not Netscape or Win3.1
    if (plgIn.indexOf("Flash")!=-1 && window.MM_flash!=null) ok=window.MM_flash;
    else if (plgIn.indexOf("Director")!=-1 && window.MM_dir!=null) ok=window.MM_dir;
    else ok=autoGo; }
  if (!ok) theURL=altURL; if (theURL) window.location=theURL;
}
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);