// JavaScript Document
// Main form valiadation function
function ValidateForm(thisform) {
var one = validate_form(thisform);
	if(one == true) {
	var two = validatePwd();
		if (two == true) {
			return true;
		} else {
			return false;
		}
	} else {
		//alert("Step 1 error");
		return false;
	}
}

function ValidateResetForm(thisform) {
var one = validatePwd(); // Do various check on password fields
if(one == false) { return false; }
return true;
}

function validate_email(field,alerttxt)
{
with (field)
{
apos=value.indexOf("@")
dotpos=value.lastIndexOf(".")
if (apos<1||dotpos-apos<2) 
  {alert(alerttxt);return false}
else {return true}
}
}

function validate_required(field,alerttxt)
{
with (field)
{
	if (value==null||value==""||value=="\[Select\]"||value=="\(000\)"||value=="\(000\)000\-0000")
	  {alert(alerttxt);return false}
	else {return true}
	}
}

function validate_agree(field,alerttxt)
{
with (field)
{
	if (checked==false)
	  {alert(alerttxt);return false}
	else {return true}
	}
}

function validate_namelength(field,alerttxt)
{
with (field)
{
	if (value.length < 4)
	  {alert(alerttxt);return false}
	else {return true}
	}
}

function validate_category(field,alerttxt)
{
with (field)
{
	if (value<10)
	  {alert(alerttxt);return false}
	else {return true}
	}
}
// End main form validation function

// Validate price field - only numbers allowed
function isNumeric(field,alerttxt) {
	var numericExpression = /^[0-9]+\.?[0-9]+$/;
	if(field.value.match(numericExpression)){
		return true;
	}else{
		alert(alerttxt);
		return false;
	}
}
// End validate price field

// Validate password fields
function validatePwd() {
var invalid = " "; // Invalid character is a space
var minLength = 6; // Minimum length
var pw1 = document.chinfo.chpassworda.value;
var pw2 = document.chinfo.chpasswordb.value;
var page = document.chinfo.page.value;
// check for a value in both fields.
	if (pw1 == '' || pw2 == '') {
		if (page == 'register'||page == 'resetpw') {
	alert('Please enter your password twice.');
	return false;
		} else {
	return true;
		}
	}
// check for minimum length
	if (document.chinfo.chpassworda.value.length < minLength) {
	alert('Your password must be at least ' + minLength + ' characters long. Try again.');
	return false;
	}
// check for spaces
	if (document.chinfo.chpassworda.value.indexOf(invalid) > -1) {
	alert("Sorry, spaces are not allowed.");
	return false;
	} else {
	if (pw1 != pw2) {
	alert ("You did not enter the same new password twice. Please re-enter your password.");
	return false;
	} else {
// alert('Nice job.');
	return true;
    }
  }
}
// End validate password fields

// Validate search fields
function validate_searchform(thisform)
{
	with (thisform)
	{
	if (validate_required(search,"Search term must be filled out!")==false)
	  {search.focus();return false}
	if (validate_category(category,"A valid category must be selected!")==false)
	  {category.focus();return false}
	}
}
// End validate search fields

// Validate add-page fields
function validate_addform(thisform)
{
	with (thisform)
	{
	if (validate_required(parish,"A specific location must be selected!")==false)
	  {parish.focus();return false}
	if (validate_required(topic,"Short description must be filled out!")==false)
	  {topic.focus();return false}
	if (validate_required(price,"You must specify a price!")==false)
	  {price.focus();return false}
	if (isNumeric(price,"You may only enter numbers and a decimal point (if required) into the price field, not $ or # etc.")==false)
	  {price.focus();return false}
	if (validate_category(category,"A valid category must be selected!")==false)
	  {category.focus();return false}
	if (validate_required(description,"Description must be filled out!")==false)
	  {description.focus();return false}
	}
}
function validate_addformguest(thisform)
{
	with (thisform)
	{
	if (validate_required(name,"Your name must be supplied!")==false)
	  {name.focus();return false}
	if (validate_namelength(name,"Your name must be a minium of 4 letters!")==false)
	  {name.focus();return false}
	if (validate_required(guest_email,"Email must be filled out!")==false)
	  {email.focus();return false}
	if (validate_required(parish,"A specific location must be selected!")==false)
	  {parish.focus();return false}
	if (validate_required(topic,"Short description must be filled out!")==false)
	  {topic.focus();return false}
	if (validate_required(price,"You must specify a price!")==false)
	  {price.focus();return false}
	if (isNumeric(price,"You may only enter numbers and a decimal point (if requried) into the price field, not $ or # etc.")==false)
	  {price.focus();return false}
	if (validate_category(category,"A valid category must be selected!")==false)
	  {category.focus();return false}
	if (validate_required(description,"Description must be filled out!")==false)
	  {description.focus();return false}
	if (validate_required(guest_pw,"A classified password must be supplied!")==false)
	  {description.focus();return false}
	if (validate_email(guest_email,"Not a valid e-mail address!")==false)
	  {email.focus();return false}
	}
}
// End validate add-page fields

// Validate register-page fields
function validate_form(thisform)
{
	with (thisform)
	{
	if (regtype==undefined) {
		alert("Please set your account type, Personal or Business user.");
		return false;
	}
	if (validate_required(username,"Username must be filled out!")==false)
	  {username.focus();return false}
	if (validate_required(email,"Email must be filled out!")==false)
	  {email.focus();return false}
	if (validate_email(email,"Your e-mail address is not valid!")==false)
	  {email.focus();return false}
	if (validate_required(chhphone,"Both phone numbers must be filled out!")==false)
	  {chhphone.focus();return false}
	if (validate_required(chcphone,"Both phone numbers must be filled out!")==false)
	  {chcphone.focus();return false}
	if (validate_required(confirm_val,"Visual code must be filled out!")==false)
	  {confirm_val.focus();return false}
	if (validate_agree(agree_tos,"You must agree to our Terms and Conditions!")==false)
	  {return false}
	if (validate_agree(agree_pp,"You must agree to our Privacy Policy!")==false)
	  {return false}
	if (regtype=="personal") {
		  if (validate_required(gender,"Gender must be selected!")==false)
		    {return false}
		  if (validate_required(gender,"Gender must be selected!")==true)
		    {return true}
	  } else if (regtype=="business") {
		  if (validate_required(company,"You are registering as a business user but have not supplied a company name!")==false)
		    {company.focus();return false}
		  if (validate_required(company,"You are registering as a business user but have not supplied a company name!")==true)
		    {return true}
	  }
	}
}
// End validate register-page fields

// Validate email-page fields
function validate_emailform(thisform)
{
	with (thisform)
	{
	if (validate_required(name,"Name must be filled out!")==false)
	  {name.focus();return false}
	if (validate_required(email,"Email must be filled out!")==false)
	  {email.focus();return false}
	if (validate_email(email,"Not a valid e-mail address!")==false)
	  {email.focus();return false}
	if (validate_required(text,"Text must be filled out!")==false)
	  {text.focus();return false}
	}
}
// End validate email-page fields

// Validate report-page fields
function validate_reportform(thisform)
{
	with (thisform)
	{
	if (validate_required(reason,"Reason must be selected!")==false)
	  {reason.focus();return false}
	if (validate_required(name,"Name must be filled out!")==false)
	  {name.focus();return false}
	if (validate_required(email,"Email must be filled out!")==false)
	  {email.focus();return false}
	if (validate_email(email,"Not a valid e-mail address!")==false)
	  {email.focus();return false}
	if (validate_required(text,"Comments must be filled out!")==false)
	  {text.focus();return false}
	}
}
// End validate report-page fields

// Validate edit-page fields
function validate_editform(thisform)
{
	with (thisform)
	{
	if (validate_required(price,"Current price must be filled out!")==false)
	  {price.focus();return false}
	if (isNumeric(price,"You may only enter numbers into the price field, not $ or # etc.")==false)
	  {price.focus();return false}
	}
}
// End validate edit-page fields

// Validate phonenumber fields
var n;
var p;
var p1;
function ValidatePhone(){
p=p1.value
if(p.length==3){
	//d10=p.indexOf('(')
	pp=p;
	d4=p.indexOf('(')
	d5=p.indexOf(')')
	if(d4==-1){
		pp="("+pp;
	}
	if(d5==-1){
		pp=pp+")";
	}
	//pp="("+pp+")";
	document.chinfo.chhphone.value="";
	document.chinfo.chhphone.value=pp;
}
if(p.length>3){
	d1=p.indexOf('(')
	d2=p.indexOf(')')
	if (d2==-1){
		l30=p.length;
		p30=p.substring(0,4);
		//alert(p30);
		p30=p30+")"
		p31=p.substring(4,l30);
		pp=p30+p31;
		//alert(p31);
		document.chinfo.chhphone.value="";
		document.chinfo.chhphone.value=pp;
	}
	}
if(p.length>5){
	p11=p.substring(d1+1,d2);
	if(p11.length>3){
	p12=p11;
	l12=p12.length;
	l15=p.length
	//l12=l12-3
	p13=p11.substring(0,3);
	p14=p11.substring(3,l12);
	p15=p.substring(d2+1,l15);
	document.chinfo.chhphone.value="";
	pp="("+p13+")"+p14+p15;
	document.chinfo.chhphone.value=pp;
	//obj1.value="";
	//obj1.value=pp;
	}
	l16=p.length;
	p16=p.substring(d2+1,l16);
	l17=p16.length;
	if(l17>3&&p16.indexOf('-')==-1){
		p17=p.substring(d2+1,d2+4);
		p18=p.substring(d2+4,l16);
		p19=p.substring(0,d2+1);
		//alert(p19);
	pp=p19+p17+"-"+p18;
	document.chinfo.chhphone.value="";
	document.chinfo.chhphone.value=pp;
	//obj1.value="";
	//obj1.value=pp;
	}
}
//}
setTimeout(ValidatePhone,100)
}
function getIt(m){
n=m.name;
//p1=document.forms[0].elements[n]
p1=m
ValidatePhone()
}
function testphone(obj1){
p=obj1.value
//alert(p)
p=p.replace("(","")
p=p.replace(")","")
p=p.replace("-","")
p=p.replace("-","")
//alert(isNaN(p))
if (isNaN(p)==true){
alert("Check phone");
return false;
}
}

var n;
var p;
var p2;
function ValidatePhone2(){
p=p2.value
if(p.length==3){
	//d10=p.indexOf('(')
	pp=p;
	d4=p.indexOf('(')
	d5=p.indexOf(')')
	if(d4==-1){
		pp="("+pp;
	}
	if(d5==-1){
		pp=pp+")";
	}
	//pp="("+pp+")";
	document.chinfo.chcphone.value="";
	document.chinfo.chcphone.value=pp;
}
if(p.length>3){
	d1=p.indexOf('(')
	d2=p.indexOf(')')
	if (d2==-1){
		l30=p.length;
		p30=p.substring(0,4);
		//alert(p30);
		p30=p30+")"
		p31=p.substring(4,l30);
		pp=p30+p31;
		//alert(p31);
		document.chinfo.chcphone.value="";
		document.chinfo.chcphone.value=pp;
	}
	}
if(p.length>5){
	p11=p.substring(d1+1,d2);
	if(p11.length>3){
	p12=p11;
	l12=p12.length;
	l15=p.length
	//l12=l12-3
	p13=p11.substring(0,3);
	p14=p11.substring(3,l12);
	p15=p.substring(d2+1,l15);
	document.chinfo.chcphone.value="";
	pp="("+p13+")"+p14+p15;
	document.chinfo.chcphone.value=pp;
	//obj1.value="";
	//obj1.value=pp;
	}
	l16=p.length;
	p16=p.substring(d2+1,l16);
	l17=p16.length;
	if(l17>3&&p16.indexOf('-')==-1){
		p17=p.substring(d2+1,d2+4);
		p18=p.substring(d2+4,l16);
		p19=p.substring(0,d2+1);
		//alert(p19);
	pp=p19+p17+"-"+p18;
	document.chinfo.chcphone.value="";
	document.chinfo.chcphone.value=pp;
	//obj1.value="";
	//obj1.value=pp;
	}
}
//}
setTimeout(ValidatePhone2,100)
}
function getIt2(m){
n=m.name;
//p1=document.forms[0].elements[n]
p2=m
ValidatePhone2()
}
function testphone2(obj2){
p=obj2.value
//alert(p)
p=p.replace("(","")
p=p.replace(")","")
p=p.replace("-","")
p=p.replace("-","")
//alert(isNaN(p))
if (isNaN(p)==true){
alert("Check phone");
return false;
}
}
// End validate phonenumber fields

// Show large image
function showLargeImage(strDisplayPath, ad_id) {
	var ad_id = ad_id ? ad_id : "";
	var image = document.getElementById("display_image" + ad_id).firstChild;
	if (navigator.userAgent.toLowerCase().indexOf('safari') > 0) {
		next_image = new Image;
		next_image.src = strDisplayPath;

		image.src = next_image.src;
		waitForNextImage(next_image, ad_id);
	} else {
		image.src = strDisplayPath;
	}
}
// End show large image

// Borders on thumbs
var styles = [];
function thumbnailBorder(thumb, image_num, ad_id) {
	var ad_id = ad_id ? ad_id : "";

	if (typeof(thumb.style) == 'undefined') {
		return false;
	}

	for (i = 0; i < image_num; i++) {
		var thumb_obj = document.getElementById('thumb' + i + ad_id);
		if (!styles[i]) {
			styles[i] = thumb_obj.className;
			if (styles[i].indexOf('ad_border_solid_black') > 0) {
				styles[i] = styles[i].substring(0, styles[i].indexOf('ad_border_solid_black')) + 'ad_border_solid_grey';
			}
		}
		thumb_obj.className = styles[i];

		if (thumb.id == thumb_obj.id) {
			thumb_obj.className = "ad_thumb ad_border_solid_black";
			document.getElementById('display_image' + ad_id).className = 'ad_pict' + ((styles[i].indexOf('ad_border_dotted') > 0)?' ad_border_dotted':'');
		}
	}
}
// End borders on thumbs
function getBrowserHeight() {
	var intH = 0;
	var intW = 0;
   
	if(typeof window.innerWidth  == 'number' ) {
	   intH = window.outerHeight + 50;
	   intW = window.outerWidth;
	} 
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		intH = document.documentElement.clientHeight;
		intW = document.documentElement.clientWidth;
	}
	else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
		intH = document.body.clientHeight;
		intW = document.body.clientWidth;
	}

	return { width: parseInt(intW), height: parseInt(intH) };
}  

function SetLayerPosition() {
	var shadow = document.getElementById("shadow");
	var question = document.getElementById("loading");

	var bws = getBrowserHeight();
	shadow.style.width = bws.width + "px";
	shadow.style.height = bws.height + "px";

	question.style.left = parseInt((bws.width - 350) / 2);
	question.style.top = parseInt((bws.height - 200) / 2);

	shadow = null;
	question = null;
}

function ShowLayer() {
	SetLayerPosition();

	var shadow = document.getElementById("shadow");
	var question = document.getElementById("loading");

	shadow.style.display = "block"; 
	question.style.display = "block";

	shadow = null;
	question = null;             
}

function HideLayer() {
	var shadow = document.getElementById("shadow");
	var question = document.getElementById("loading");

	shadow.style.display = "none"; 
	question.style.display = "none";

	shadow = null;
	question = null; 
}

var regtype;
function toggle(type) {
	if(type=="business") {
		document.getElementById("companyrow").style.display="";
		document.getElementById("genderrow").style.display="none";
		document.getElementById("gender").value="N/A";
		document.getElementById('phonename').innerHTML = 'Office';
		regtype = "business";
	}
	else if(type=="personal") {
		document.getElementById("companyrow").style.display="none";
		document.getElementById("company").value="";
		document.getElementById("genderrow").style.display="";
		document.getElementById('phonename').innerHTML = 'Home';
		regtype = "personal";
	}
}
