var NumberOnlyMessage;
var NoModelSelectedMessage;
var ValidEmailMessage;
var NotEmptyMessage;
var StringOnlyMessage;
var NotEqualMessage;

function DeleteList() {
	document.getElementById("data_models").length = 0
	document.getElementById("h_models").value=""
}

function NumberOnly(id) {

	var elm = document.getElementById(id)
	var val = document.getElementById(id).value
	var match = /^\d+$/.test(val)
	if (val=="")
	match=1
	if (!match) {
		alert (NumberOnlyMessage);
		elm.style.backgroundColor = "#eeeeee"
		elm.focus()
		return false
	}
	return true
}

function ValidEmail(id) {
	var elm = document.getElementById(id)
	var val = document.getElementById(id).value
	var match = /^\S+\@\w+(\.\w+$)|(\.\w+\.\w+$)/.test(val)
	if (!match)  {
		alert (ValidEmailMessage)
		elm.style.backgroundColor = "#eeeeee"
		elm.focus()
		return false	}
		return true
}

function NotEmpty(id) {
	var elm = document.getElementById(id)
	var val = document.getElementById(id).value
	if (val=="") {
		alert (NotEmptyMessage)
		elm.style.backgroundColor = "#eeeeee"
		elm.focus()
		return false	
	}
	return true
}

function NotEqual(id1,id2)
{
	var elm1=document.getElementById(id1)
	var val1=elm1.value
	var elm2=document.getElementById(id2)
	var val2=elm2.value
	if (val1!=val2) {
		alert (NotEqualMessage)
		elm1.style.backgroundColor="#eeeeee"
		elm2.style.backgroundColor="#eeeeee"
		elm2.focus()
		return false
	}
	return true
}

function NotEmptyList(id)
{
	var elm=document.getElementById(id)
	if (elm.options.length>0) return true
	alert(NotEmptyMessage)
	elm.style.backgroundColor="#eeeeee"
	elm.focus()
}

function StringOnly(id) {
	var elm = document.getElementById(id)
	var val = document.getElementById(id).value
	var match = /^\w+$/.test(val)
	if (val=="")
	match=1
	if (!match) {
		alert (StringOnlyMessage)
		elm.style.backgroundColor = "#eeeeee"
		elm.focus()
		return false		}
		return true
}

function PostData() {
	var y = document.getElementById("data_models")
	var hdn_town = document.getElementById("h_models")
	var temp = ""
	for (i=0;i<y.length;i++)
	temp = temp+y.options[i].value+","
	hdn_town.value = temp
}




function FormValidate(id) {

	switch (id)
	{
		case "pref":
		if(!NotEmpty("onshow") || !NotEmpty("onurgent") || !NumberOnly("onshow") || !NumberOnly("onurgent") || !NumberOnly("motorshow") || !NumberOnly("motorurgent"))
		return false
		break

		case "addauc":
		case "editauc":
		case "edituserauc":
		case "addmotor":
		case "editmotor":
		case "editusermotor":
		if (!NotEmpty("make") || !NotEmpty("model") || !NotEmpty ("type") || !NotEmpty("body") || !NotEmpty("fuel") || !NotEmpty("transmission")  || !NumberOnly("engine") || !NumberOnly("volume") || !NotEmpty("color") || !NotEmpty("year") || !NotEmpty("drive") || !NotEmpty("price") || !NotEmpty("currency") || !NotEmpty("sellertype"))
		return false
		break
		
		case "sendauc":
		case "sendmotor":
		if (!NotEmpty("make") || !NotEmpty("model") || !NotEmpty ("type") || !NotEmpty("body") || !NotEmpty("fuel") || !NotEmpty("transmission")  || !NumberOnly("engine") || !NumberOnly("volume") || !NotEmpty("color") || !NotEmpty("year") || !NotEmpty("drive") || !NotEmpty("price") || !NotEmpty("currency") || !NotEmpty("sellertype") || !NotEmpty("firstname") || !NotEmpty("surname") || !NotEmpty("email") || !NotEmpty("phone") || !NotEmpty("cell"))
		return false
		break

		case "support" :
		if (!NotEmpty("isim") || !NotEmpty("soyisim") || !NotEmpty("mesaj"))
		return false
		break

		case "adddetail":
		case "addmodel":
		case "addmake":
		if (!NotEmpty ("label"))
		return false
		break

		case "searchauc" :
		if (!NumberOnly ("m2"))
		return false
		PostData();
		break

		case "searchmotor" :
		if (!NumberOnly ("m2"))
		return false
		PostData();
		break

		case "detayli" :
		if (!NotEmpty ("isim")||!ValidEmail("email"))
		return false
		document.getElementById("link1").value = document.location.href
		break

		case "arkgonder" :
		if (!NotEmpty ("isim1") || !NotEmpty("isim2") ||  ! ValidEmail ("email1") || ! ValidEmail("email2"))
		return false
		document.getElementById("link2").value = document.location.href
		break

		case "editcur" :
		var elm = document.getElementById("ratio")
		var val = document.getElementById("ratio").value
		var match = /(^\d+\.\d+$)|(^\d+$)/.test(val)
		if (!match) {
			elm.style.backgroundColor = "#eeeeee"
			elm.focus()
			return false
		}
		break

		case "addrequest" :
		if(!NotEmpty("make_r") || !NotEmptyList("data_models") || !NotEmpty("type_r") || !NotEmpty("price1") || !NotEmpty("price2") || !NotEmpty("currency"))
		{
			return false;
		}
		PostData();
		break

		case "editrequest":
		PostData()
		if(!NotEmpty("make_r") || !NotEmptyList("data_models") || !NotEmpty("type_r") || !NotEmpty("price1") || !NotEmpty("price2") || !NotEmpty("currency"))
		return false;
		break
		
		case "addpage":
		case "editpage":
		if(!NotEmpty("label") || !NotEmpty("main"))
		return false;
		PostData();
		break;
		
		case "addclient":
		case "editclient":
		if (!NotEmpty("firstname") || !NotEmpty("surname") || !ValidEmail("email"))
		return false
		PostData()
		break
		
		case "editcategory":
		case "addcategory":
		if (!NotEmpty("label") || !NotEmpty("sortOrder") || !NumberOnly("sortOrder"))
		return false
		PostData()
		break
		
		case "addarticle":
		case "editarticle":
		if (!NotEmpty("topic") || !NotEmpty("body"))
		return false
		PostData()
		break
		
		case "secpref":
		if (!NotEmpty("username") || !NotEmpty("pass") || !NotEmpty("pass2") || !NotEqual("pass","pass2") || !NotEmpty("code"))		
		return false
		PostData()
		break

	} //end switch

	return true

} // end function


function AlreadyAtList(element) {

	y =document.getElementById("data_models")
	for (i = 0; i < y.length; i++) {
		value = y.options[i].value;
		if (element == value) return true;
	}
	return false;
}

function AddToList(NoModelSelected) {
	x = document.getElementById("data_model")
	y = document.getElementById("data_models");
	h = document.getElementById("h_models");
	if (x.selectedIndex==-1) {
		alert (NoModelSelected)
		return false
	}
	if (AlreadyAtList(x.options[x.selectedIndex].value)) exit()
	
	y.length++
	y.options[y.length-1].value= x.options[x.selectedIndex].value
	y.options[y.length-1].text= x.options[x.selectedIndex].text
	h.value+=","+x.options[x.selectedIndex].value
}

function DeleteFromList(NoModelSelected) {
	y = document.getElementById("data_models")
	h = document.getElementById("h_models")
	if (y.selectedIndex==-1){
		alert (NoModelSelectedMessage)
		return false
	}

	if(y.length==1){
		DeleteList()
		return
	}
	else if (y.selectedIndex==y.length-1){
		y.length--
		h.value=h.value.substring(0,h.value.lastIndexOf(","))
		return
	}

	else if (y.selectedIndex==-1){
		alert (NoModelSelectedMessage)
		return
	}
	else {
		hv=h.value.substring(1)
		hvs=hv.split(",")
		for (i=y.selectedIndex;i<y.length-1;i++) {
			y.options[i] = y.options[i+1]
			hvs[i]=hvs[i+1]
		}
		hvs.length--;
		h.value="";
		for(i=0;i<hvs.length;i++)
			h.value+=","+hvs[i]
	}

	return true

}

function AddAll() {
	x = document.getElementById("data_model")
	y = document.getElementById("data_models");
	h = document.getElementById("h_models")
	DeleteList()
	for (i=0; i<x.length; i++) {
		y.length = x.length
		y.options[i].value = x.options[i].value
		y.options[i].text = x.options[i].text
		h.value+=","+x.options[i].value
	}
}
