var urlAddress = "http://www.carads.co.za";
var pageName = "www.carads.co.za";
function addToFavorites() { 
	if (window.external) { 
	window.external.AddFavorite(urlAddress,pageName) 
	} 
	else { 
	alert("Sorry! Your browser doesn't support this function."); 
	} 
}

function isNumber(s) {
	if(s.match(/^\d{0,10}$/) && s <= 2147483647)
		return true;
	else
		return false ;
}

function emailvalid(s)
{
	if (s.match(/^\w+([\.-]?\w+)*[\.]?@\w+([\.-]?\w+)*(\.\w{2,4})+$/i))
		return true;
	else
		return false;
}

function isblank(s)
{
	for(var i=0; i < s.length; i++)
	{
		var c=s.charAt(i);

		if((c != " ") && (c != "\n") && (c != "\t"))
			return false;
	}
	return true;
}

//Function to open new pop up window with variable width and height
function openNew(img_nm,w,h,nm)
{

	var myfeatures="directories=no,location=no,menubar=no,status=yes,titlebar=no,toolbar=no,resizable=yes,scrollbars=yes,width="+w+",height="+h+",left=0,top=0";

	if(nm == "")
		nm="new";

	var newImg=open(img_nm,nm,myfeatures);
	newImg.focus();
}

//Function to open new pop up window with fixed size
function openWin(win_nm,nm)
{
	var h=screen.height;
	h=h-80;

	var myfeatures="directories=no,location=no,menubar=no,status=yes,titlebar=no,toolbar=no,resizable=yes,scrollbars=yes,width=680,height="+h+",left=0,top=0";

	if(nm == "")
		nm="new";

	var newImg=window.open(win_nm,nm,myfeatures);
	newImg.focus();
}

function CheckOffer() {
year = eval("document.PostForm.year");
	if(!isNumber(year.value)){
    alert('Please Enter Digit only.');year.focus();return false;}
	
	/*if(isblank(document.PostForm.ProductType.value))
	{
		alert('Please select the advert type.');
		document.PostForm.ProductType.focus();
		return false;
	}*/
	if(isblank(document.PostForm.CategoryID.value))
	{
		alert('Please select the category.');
		document.PostForm.CategoryID.focus();
		return false;
	}
	if(isblank(document.PostForm.Description.value))
	{
		alert('Add description please.');
		document.PostForm.Description.focus();
		return false;
	}
	if(document.getElementById('pass') && document.getElementById('pass').checked)
	{
	if((document.PostForm.username)){
		if(isblank(document.PostForm.username.value))
		{
			alert('Add Username please.');
			document.PostForm.username.focus();
			return false;
		}
	}
	if((document.PostForm.password)){
		if(isblank(document.PostForm.password.value))
		{
			alert('Add Password please.');
			document.PostForm.password.focus();
			return false;
		}
		if(isblank(document.PostForm.confpassword.value))
		{
			alert('Add Confirm Password please.');
			document.PostForm.confpassword.focus();
			return false;
		}
		if(document.PostForm.password.value != document.PostForm.confpassword.value)
		{
			alert('Password and Confirm Password note matched.');
			document.PostForm.password.focus();
			return false;
		}
	
	}
}

}


function CheckProfile() {
	if(isblank(document.RegForm.p1.value))
	{
		window.alert('Enter your password, please!');
		document.RegForm.p1.focus();
		return false;
	}
	if(isblank(document.RegForm.p2.value))
	{
		window.alert('Confirm your password, please!');
		document.RegForm.p2.focus();
		return false;
	}
	if(document.RegForm.p1.value != "" && document.RegForm.p2.value != "" && document.RegForm.p1.value != document.RegForm.p2.value)
	{
		window.alert('Enter and confirm your password again!');
		document.RegForm.p1.value="";
		document.RegForm.p2.value="";
		document.RegForm.p1.focus();
		return false;
	}
	if(isblank(document.RegForm.FirstName.value))
	{
		window.alert('Enter your First Name, please!');
		document.RegForm.FirstName.focus();
		return false;
	}
	if(isblank(document.RegForm.LastName.value))
	{
		window.alert('Enter your Last Name, please!');
		document.RegForm.LastName.focus();
		return false;
	}
	if(isblank(document.RegForm.Phone.value))
	{
		window.alert('Enter your Phone, please!');
		document.RegForm.Phone.focus();
		return false;
	}
	if(document.RegForm.MyAgree.checked==false)
	{
		window.alert('Select agreement, please!');
		document.RegForm.MyAgree.focus();
		return false;
	}
	if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.RegForm.email.value))
	{
		return true;
	}
	alert("Invalid E-mail Address! Please re-enter.");
	document.RegForm.email.focus();
	return false;
}

function CheckLogin() {
	if(isblank(document.lform.us.value))
	{
		window.alert('Enter your username, please!');
		document.lform.us.focus();
		return false;
	}
	if(isblank(document.lform.ps.value))
	{
		window.alert('Enter your password, please!');
		document.lform.ps.focus();
		return false;
	}
}

function CheckForgot() {
	if(isblank(document.ForgotForm.u2.value))
	{
		window.alert('Enter your username, please!');
		document.ForgotForm.u2.focus();
		return false;
	}
}

function CheckSearch() {
	if(isblank(document.SearchForm.what.value))
	{
		window.alert('Enter search term, please!');
		document.SearchForm.what.focus();
		return false;
	}
}

function CheckRegister() {
	if(isblank(document.RegForm.NewUsername.value))
	{
		window.alert('Enter your username, please!');
		document.RegForm.NewUsername.focus();
		return false;
	}
	if(isblank(document.RegForm.p1.value))
	{
		window.alert('Enter your password, please!');
		document.RegForm.p1.focus();
		return false;
	}
	if(isblank(document.RegForm.p2.value))
	{
		window.alert('Confirm your password, please!');
		document.RegForm.p2.focus();
		return false;
	}
	if(document.RegForm.p1.value != "" && document.RegForm.p2.value != "" && document.RegForm.p1.value != document.RegForm.p2.value)
	{
		window.alert('Enter and confirm your password again!');
		document.RegForm.p1.value="";
		document.RegForm.p2.value="";
		document.RegForm.p1.focus();
		return false;
	}
	if(isblank(document.RegForm.FirstName.value))
	{
		window.alert('Enter your First Name, please!');
		document.RegForm.FirstName.focus();
		return false;
	}
	if(isblank(document.RegForm.LastName.value))
	{

		window.alert('Enter your Last Name, please!');
		document.RegForm.LastName.focus();
		return false;
	}
	if(isblank(document.RegForm.Phone.value))
	{
		window.alert('Enter your Phone, please!');
		document.RegForm.Phone.focus();
		return false;
	}
	if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.RegForm.email.value))
	{
		return true;
	}
	alert("Invalid E-mail Address! Please re-enter.");
	document.RegForm.email.focus();
	return false;
}
function CheckContact() {

		if(isblank(document.contact.u_name.value))
		{
			alert('Enter your name, please!');
			document.contact.u_name.focus();
			return false;
		}

		if(isblank(document.contact.u_email.value))
		{
			alert('Enter your email, please!');
			document.contact.u_email.focus();
			return false;
		}

		if(isblank(document.contact.subject.value))
		{
			alert('Enter the subject, please!');
			document.contact.subject.focus();
			return false;
		}

		if(isblank(document.contact.message.value))
		{
			alert('Enter your message, please!');
			document.contact.message.focus();
			return false;
		}

}

//function to action on change category
function categChange(surl,opti){
	if(document.PostForm.CategoryID.value==40){
		window.location.href=surl+"&opt=club&CategoryID=40";
	}else if(opti==40){
		window.location.href=surl+"&CategoryID="+document.PostForm.CategoryID.value;
	}
}


function Selectmodel(sform1){
	sfrm1 = eval("document."+sform1);
// ON selection of category this function will work
removeAllOptions(sfrm1.type_ext1);
addOption(sfrm1.type_ext1, "", "Select Model", "");

var makeval = sfrm1.make1.value; 
var model_length = eval('modelarr'+makeval).length;
for(var ml=0 ; ml < model_length ; ml++){
	addOption(sfrm1.type_ext1,eval('modelvarr'+makeval+'['+ml+']'), eval('modelarr'+makeval+'['+ml+']'));
}

}

function Selectmodel1(sform1){
	sfrm1 = eval("document."+sform1);
// ON selection of category this function will work
removeAllOptions(sfrm1.type_ext);
addOption(sfrm1.type_ext, "", "Select Model", "");

var makeval = sfrm1.make.value; 
var model_length = eval('modelarr'+makeval).length;
for(var ml=0 ; ml < model_length ; ml++){
	addOption(sfrm1.type_ext,eval('modelvarr'+makeval+'['+ml+']'), eval('modelarr'+makeval+'['+ml+']'));
}

}

////////////////// 

function removeAllOptions(selectbox)
{
	var i;
	for(i=selectbox.options.length-1;i>=0;i--)
	{
		//selectbox.options.remove(i);
		selectbox.remove(i);
	}
}


function addOption(selectbox, value, text )
{
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;

	selectbox.options.add(optn);
}