// JavaScript Document
/*------------------------------------------------------------------
AutoSite Version 1.0 - Online Vehicle Inventory Manager
AutoSite is (c) Copyright 2005 myTopTools.com.  All Rights Reserved.
------------------------------------------------------------------*/

/// verify required fields
function checkrequired(form) {
	var pass = true;
	for(i = 0; i < form.length; i++) {
		var tempobj = form.elements[i];
		if(tempobj.alt == "required") {
			if(tempobj.value == '') {
				pass = false;
				break;
			}
		}
	}
	if(!pass) {
		alert("Please enter the required information.");
		tempobj.focus(); // set focus to missing field
		return false;
	}
	else { return true; } 
}

// image swap for view page
function swap(target, fname) {
	document[target].src = fname;
}

// make sure at least one search criteria has been provided
function checksearch(form) {
	var count = 0;
	for(i = 0; i < form.length; i++) {
		var tempobj = form.elements[i];
		if(tempobj.value == '') {
			count++;
		}
	}
	i-=2;

	if(count == i) {
		alert("You have not entered any search criteria.");
		return false;
	}
	else {
		//alert("You DID enter search criteria." + count + "  " + i);
		return true;
	} 
}

/// confirm removal of vehicle listing
function verify_removal(ccode) {
	msg = "Are you sure you want to remove this vehicle listing?";
	if(confirm(msg)) {
		window.location='remove.php?ccode=' + ccode;
	} else {
		return false;
	}
}

/// confirm removal of dealer
function verify_dealer_removal(ccode) {
	msg = "Are you sure you want to remove this dealer from your list?";
	if(confirm(msg)) {
		window.location='remove_dealer.php?did=' + ccode;
	} else {
		return false;
	}
}

// /confirm removal of table entry
function verify_entry(ccode,table) {
	msg = "Are you sure you want to remove this entry?";
	if(confirm(msg)) {
		window.location='remove_entry.php?ccode='+ccode+'&table='+table;
	} else {
		return false;
	}
}

// /confirm removal of image from vehicle listing
function verify_image(imageid, ccode) {
	msg = "Are you sure you want to remove this image?";
	if(confirm(msg)) {
		window.location='removeimg.php?id=' + imageid + '&ccode=' + ccode;
	} else {
		return false;
	}
}
// /confirm removal of featured image from vehicle listing
function verify_featured(imageid, ccode) {
	msg = "Are you sure you want to set this image as featured?";
	if(confirm(msg)) {
		window.location='featuredimg.php?id=' + imageid + '&ccode=' + ccode;
	} else {
		return false;
	}
}
/// display invalid stock number message
function invalid_stock(stock) {
	msg = "The stock number " + stock + " appears to be invalid.";
	alert(msg);
	window.location='index.php';
	/*
	if(confirm(msg)) {
		window.location='index.php';
	} else {
		window.location='index.php';
	}
	*/	
}

/// display vin number message
function invalid_vin(vin) {
	msg = "The VIN number " + vin + " appears to be invalid.";
	alert(msg);
	window.location='index.php';
	/*
	if(confirm(msg)) {
		window.location='index.php';
	} else {
		window.location='index.php';
	}
	*/	
}

/// display car number message
function invalid_car_id(car_id) {
	msg = "The Car ID number " + car_id + " appears to be invalid.";
	alert(msg);
	window.location='index.php';
	/*
	if(confirm(msg)) {
		window.location='index.php';
	} else {
		window.location='index.php';
	}
	*/	
}

// check passwords
function check(){
	if((document.form1.pass1.value!=document.form1.pass2.value)||(document.form1.pass2.value=='')||(document.form1.pass1.value=='')){
		window.alert('Wrong password confirmation or fields are empty.');
	}
	else{
	document.form1.submit();
	}                                  
}

/// confirm removal of emails listing
function verify_clean_emails() {
	msg = "Are you sure you want to remove collected emails from database?";
	if(confirm(msg)) {
		window.location='remove_emails.php';
	} else {
		return false;
	}
}


// download popup
function export_popup(URL){
    var Width = 400, Height = 200;
    var xMax = screen.width, yMax = screen.height;
    Top = (yMax - Height)/2 - 40;
    Left = (xMax - Width)/2;        
    var windowName = 'export_popup';    
    var features =
    'width='        + Width +
    ',height='      + Height +
    ',directories=' + 0 +
    ',location='    + 0 +
    ',menubar='     + 0 +
    ',scrollbars='  + 0 +
    ',status='      + 0 +
    ',toolbar='     + 0 +
    ',resizable='   + 0 +
    ',top='     + Top +
    ',left='     + Left;
    window.open(URL, windowName, features);
}

/// search jump menu
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='?mid="+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function validateTextNumber() {
	e=0;
	if(IsNumeric(document.forms[0].elements["f[year_from]"].value)==false){e=1;}
	if(IsNumeric(document.forms[0].elements["f[year_till]"].value)==false){e=1;}
	if(IsNumeric(document.forms[0].elements["f[price_from]"].value)==false){e=1;}
	if(IsNumeric(document.forms[0].elements["f[price_till]"].value)==false){e=1;}
	if(e==1){
		alert("Search fields must contain whole numbers only.\nNo commas or special characters allowed.\n(Example: 99999)");
	}
	else{
		document.forms[0].submit();	
	}
}

// whole number check
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;
}
