// **********************************   common validation functions	 **********************************************	
function chk_empty(bx){
	if(bx.value==""){
		var nm=bx.name;
		//var b_nm=nm[1].toUpperCase();
		alert("The field \'"+nm+"\' cannot be empty. Please enter a valid value");
		bx.focus();
		return false;
	}
	else{return true;}	
}

function is_selected(cbo){
	if(cbo.value=="EMPTY"){
		var nm=cbo.name.split("_");
		var b_nm=nm[1].toUpperCase();
		alert("The field \'"+b_nm+"\' cannot be empty. Please Select an aritist form the drop down");
		cbo.focus();
		return false;
	}
	else{return true;}	
}
/*function validEmail(formField){
	if(!isEmailAddr(formField.value)) 
	{
	alert("Please enter a complete email address in the form: yourname@yourdomain.com");
	formField.focus();
	return  false;
	}
else{return true;}	
}*/

function checkEmail(cho) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(cho.value)){
return (true);
}
alert("Invalid E-mail Address! Please re-enter.");
cbo.focus();
return (false)
}
// JavaScript Document
function makeObject(){
	var x; 
	var browser = navigator.appName; 
	//detect the client browser
	if(browser == "Microsoft Internet Explorer"){
		x = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else{
		x = new XMLHttpRequest();
	}
	
	return x;
}
	//call the function makeObject()
		//call the function makeObject()	
	//call the function makeObject()
var request = makeObject();
var md="";
var popupwindow;
function login_submit(){
	if((document.admin_login.username.value!="")&&(document.admin_login.password.value!="")){
		document.admin_login.action="process.php?val=login";
		document.admin_login.submit();
	}
	else{alert("Username and Password cannot be empty. Please enter valid values");}	
}
var popupwindow;
function show_large(ne){
	popupwindow=window.open('view_large.php?val='+ne,'popup','height=325,width=450,scrollbars');
	popupwindow.focus();
}
function search_area(){
	document.search_a.action="search.php";
	document.search_a.submit();
	return true;
}

function display_price(){
	document.all.price_value.style.display="inline";
	
}
function hide_price(){
	document.all.price_value.style.display="none";
	
}
function display_rent(){
	document.all.rent.style.display="inline";
	
}
function hide_rent(){
	document.all.rent.style.display="none";
	
}
function display_pricearea(){
	document.all.pricearea.style.display="inline";
	
}
function hide_pricearea(){
	document.all.pricearea.style.display="none";
	
}
function display_unreg(){
	document.all.unreg.style.display="inline";
	
}
function hide_unreg(){
	document.all.unreg.style.display="none";
	
}
function display_unregtext(){
	document.all.unregtext.style.display="inline";
	
}
function hide_unregtext(){
	document.all.unregtext.style.display="none";
	
}

function display_image(){
	document.all.main_tr.style.display="inline";
	for(var x=1;x<10;x++){
		var img_name='vehicle_img'+x;
		var td_name='vehicle'+x;
		var el = document.getElementById(img_name);
		var tl = document.getElementById(td_name);
		el.style.display="inline";
		tl.style.display="inline";
	}
	
}
function hide_image(){
	document.all.main_tr.style.display="none";
	for(var x=1;x<10;x++){
		var img_name='vehicle_img'+x;
		var td_name='vehicle'+x;
		var el = document.getElementById(img_name);
		var tl = document.getElementById(td_name);
		el.style.display="none";
		tl.style.display="none";
	}
	
}
/*function add_stock(){
	if((is_selected(document.add_vehicle.addvehicle_type))&&(is_selected(document.add_vehicle.addvehicle_brand))&&(chk_empty(document.add_vehicle.model))&&(chk_empty(document.add_vehicle.year))&&(chk_empty(document.add_vehicle.description))&&(chk_empty(document.add_vehicle.price))&&(chk_empty(document.add_vehicle.name))&&(chk_empty(document.add_vehicle.address))&&(chk_empty(document.add_vehicle.city))&&(chk_empty(document.add_vehicle.telephone)))
	{
			document.add_vehicle.action="process.php?val=ADD_STOCK";
			document.add_vehicle.submit();
			return true;
	}
	else{return false;}	
}*/
function add_stock(){
	if((chk_empty(document.add_vehicle.title))&&(chk_rent())&&(is_selected(document.add_vehicle.addvehicle_type))&&(is_selected(document.add_vehicle.addvehicle_brand))&&(chk_empty(document.add_vehicle.model))&&(chk_empty(document.add_vehicle.year))&&(is_selected(document.add_vehicle.addvehicle_country))&&(chk_empty(document.add_vehicle.description))&&(chk_img())&&(chk_empty(document.add_vehicle.name))&&(chk_empty(document.add_vehicle.address))&&(chk_empty(document.add_vehicle.city))&&(chk_empty(document.add_vehicle.telephone)))
	{
	document.add_vehicle.action="process.php?val=ADD_STOCK";
	document.add_vehicle.submit();
	return true;	
	}
	else{return false;}	
	
}
function chk_rent(){
	if(document.add_vehicle.sell_rent[1].checked){
		if((chk_empty(document.add_vehicle.perkm))&&(chk_empty(document.add_vehicle.permonth))&&(chk_empty(document.add_vehicle.daily))){
		  return true;
			}
	}
		else{return true;}		
}
function chk_img(){
		if(document.add_vehicle.photos[0].checked){
			var i=1;
			var all_empty=1;
			var all_jpg=1;
			var img1_emty=1;
    		for( x=1; x<9; x++){	
			var name='vehicle_img'+x;
			if(x==1){
				if (document.getElementById(name).value==""){
					img1_emty=0;
				}
			}
				if (document.getElementById(name).value!=""){
		 		var filename = document.getElementById(name).value;
		 		 var fileext = filename.substring(filename.lastIndexOf('.')+1);
					all_empty=0;			
					var b = String(fileext).toUpperCase();
					if ((b != 'JPG')) {
					 all_jpg=0;
					alert("You can only upload .jpg & .jpeg images , Image \'"+name+"\' is invalid ");                 	    
					        } 	
						}	
				}			
				if (img1_emty!=1){
			alert("Image 01 Must be Selected");
				}
				else if (all_jpg!=0){	
				return true;
		}
else{
	return false;
	}			
		
		}
		
	   
	else{return true;}	
}
function add_photos(){
	var i=1;
	var all_empty=1;
	var all_jpg=1;
    for( x=1; x<9; x++){	
	var name='addphoto_img'+x;
	
	if (document.getElementById(name).value!=""){
		
		 var filename = document.getElementById(name).value;
		  var fileext = filename.substring(filename.lastIndexOf('.')+1);
			all_empty=0;			
			var b = String(fileext).toUpperCase();
			if ((b != 'JPG')) {
					 all_jpg=0;
				alert("You can only upload .jpg & .jpeg images , Image \'"+name+"\' is invalid ");                 	    
					        } 	
					}	
			}			
if (all_empty!=0){
		alert("Select a Image to Upload");
				}
else if (all_jpg!=0){	
				document.addphotos.action="process.php?val=ADD_PHOTOS";
				document.addphotos.submit();
				return true;
		}
else{
	return false;
	}			
}
/////////////////////Ajax Polulate functions/////////////

function get_brand_list(){
	
	if(document.add_vehicle.addvehicle_type.value=="EMPTY"){
		//document.search1.search_state.disabled=true;																								
		document.all.vehicle_brand.innerHTML="";
	}
	else{
		request.open('get', 'process.php?val=get_typelist&brand_id='+document.add_vehicle.addvehicle_type.value);
		request.onreadystatechange = brand_to_select; 
		request.send('');
	}
}
function brand_to_select(){
	if (request.readyState == 1) {
		document.all.vehicle_brand.innerHTML="Loading.....";
		
		//document.search1.search_submit.disabled=true;
	}
	if (request.readyState == 4) {
		document.all.vehicle_brand.innerHTML="";
		document.all.vehicle_brand.innerHTML="";
		
    	if (request.status == 200) {
			//document.search1.search_submit.disabled=false;
			document.all.vehicle_brand.innerHTML=request.responseText;	
			
			
		}
		else {
    		alert('There was a problem with the request.');
		}
	}		
}

//////////////////////////change big image/////////////////////
function get_big_image(id){
	
	if(document.view_vehicle.small_img.value=="EMPTY"){
		//document.search1.search_state.disabled=true;																								
		document.all.small_img.innerHTML="";
	}
	else{
		request.open('get', 'process.php?val=get_big_image&id='+id);
		request.onreadystatechange = big_image; 
		request.send('');
	}
}
function big_image(){
	if (request.readyState == 1) {
		document.all.big_img.innerHTML="Loading.....";
		
		//document.search1.search_submit.disabled=true;
	}
	if (request.readyState == 4) {
		document.all.big_img.innerHTML="";
		document.all.big_img.innerHTML="";
		
    	if (request.status == 200) {
			//document.search1.search_submit.disabled=false;
			document.all.big_img.innerHTML=request.responseText;	
			
			
		}
		else {
    		alert('There was a problem with the request.');
		}
	}		
}
var slideCache = new Array();

function RunSlideShow(pictureName,imageFiles,displaySecs)

{

  var imageSeparator = imageFiles.indexOf(";");

  var nextImage = imageFiles.substring(0,imageSeparator);

  if (document.all)

  {

    document.getElementById(pictureName).style.filter="blendTrans(duration=2)";

    document.getElementById(pictureName).filters.blendTrans.Apply();

  }

  document.getElementById(pictureName).src = nextImage;

  if (document.all)

  {

    document.getElementById(pictureName).filters.blendTrans.Play();

  }

  var futureImages= imageFiles.substring(imageSeparator+1,imageFiles.length)

    + ';' + nextImage;

  setTimeout("RunSlideShow('"+pictureName+"','"+futureImages+"',"+displaySecs+")",

    displaySecs*1000);

  // Cache the next image to improve performance.

  imageSeparator = futureImages.indexOf(";");

  nextImage = futureImages.substring(0,imageSeparator);

  if (slideCache[nextImage] == null) {

    slideCache[nextImage] = new Image;

    slideCache[nextImage].src = nextImage;

  }

}
//////////////////////////////////////////
