// JavaScript Document
function stab(tab){
		for(i=1;i<=5;i++){
			if(i==tab){
				document.getElementById("step"+i).style.display="block"
			}else{
				document.getElementById("step"+i).style.display="none"				
			}
	}
}

function showPlaceAd(tab){
	frm			=	eval(document.placeAdFrm)
	errorno		=	0
	errormsg	=	"Error while posting advertisement\n"
	error		=	false

	mode		=	document.modec.mode.value

	
switch(tab){
		case 2:{
		//tab 1 validation
			if(frm.category.value==''){
				errorno++
				errormsg+="\t"+errorno+") Please select the category\n"
				error=true
			}
			if(frm.county.value==''){
				errorno++
				errormsg+="\t"+errorno+") Please select the county\n"
				error=true
			}		
			break;
		//tab 1 validation		
			}

		case 3:{
		//tab 2 validation
					if(frm.Title.value==''){
						errorno++
						errormsg+="\t"+errorno+") Please enter the Ad Title\n"
						error=true
					}
					if(frm.Description.value==''){
						errorno++
						errormsg+="\t"+errorno+") Please enter the Ad Description\n"
						error=true
					}else{
						if(frm.Description.value.length > 1000){
							errorno++
							errormsg+="\t"+errorno+") Ad Description characters is greater than 1000. Please shorten it.\n"
							error=true			
						}
					}
					if(frm.Price.value==''){
						//errorno++
						//errormsg+="\t"+errorno+") Please enter the Ad Price\n"
						//error=true
					}else{
						price	=	parseInt(frm.Price.value)
						//alert(price)
						if(price <= 0 || isNaN(price)){
							//errorno++
							//errormsg+="\t"+errorno+") Please enter the Ad Price in numeric format\n"
							//error=true			
						}
					}					
				
			break;
		//tab 2 validation		
			}
			
			case 4:
				{
					if(mode==0){						
							if(frm.Photo1.value == '' && frm.Photo2.value == '' && frm.Photo3.value == ''){
								ans	=	confirm("There is no photo uploaded.\n Are you sure you want to go to next step.\n Press OK to continue or Cancel to abort and upload photo.")
								if(ans==false){
									error=true									
								}
							}
							if(frm.Video.value == '' ){
								ans	=	confirm("There is no video uploaded.\n Are you sure you want to go to next step.\n Press OK to continue or Cancel to abort and upload video.")
								if(ans==false){
									error=true									
								}
							}	
						}
					break;
				}
			case 5:
				{
						if(frm.Name.value==''){
							errorno++
							errormsg+="\t"+errorno+") Please enter the Name\n"
							error=true
						}
						if(frm.Phone.value==''){
							errorno++
							errormsg+="\t"+errorno+") Please enter the Phone Number\n"
							error=true
						}else{
							//alert(parseInt(frm.Phone.value))
							if(isNaN(frm.Phone.value)){
								errorno++
								errormsg+="\t"+errorno+") Please enter the numeric Phone Number\n"
								error=true			
							}else{
								if(frm.Phone.value.length < 9 || frm.Phone.value.length > 10){
								errorno++
								errormsg+="\t"+errorno+") Please enter the 9 digit landline number or 10 digit mobile number\n"
								error=true			
								}else{
									if(frm.Phone.value.substr(0,2)!='01' && frm.Phone.value.substr(0,2)!='08'){
										errorno++
										errormsg+="\t"+errorno+") Please enter the correct phone number\n"
										error=true
									}
								}
							}
						}	
						
						email	= frm.Email.value
						if(echeck(email)==false){
								errorno++
								errormsg+="\t"+errorno+") Invalid Email Address\n"
								error=true
						}
						if(frm.eme.value!=null){
							if(frm.eme.value==1){
									errorno++
									errormsg+="\t"+errorno+") Enter another email address as this email address is already taken.\n"
									error=true							
							}
						}
						pass	=	frm.Password.value
						pass2	=	frm.Password2.value						
						if(pass==''){
								errorno++
								errormsg+="\t"+errorno+") Please enter the password\n"
								error=true										
						}
						if(pass2==''){
								errorno++
								errormsg+="\t"+errorno+") Please confirm the password\n"
								error=true										
						}
						if(pass!=pass2){
								errorno++
								errormsg+="\t"+errorno+") Passwords do not match\n"
								error=true										
						}
				break;
				}
			
		}
			
		if(error==true){
			if(errorno > 0){
			alert(errormsg)	
			}
		}else{
			stab(tab)
		}
}

function review_an_ad(){
	frm	=	document.placeAdFrm
	Type		=	frm.Type.value
	Category	=	frm.category.value
	County		=	frm.county.value
	Title		=	frm.Title.value
	Description	=	frm.Description.value
	Price		=	frm.Price.value
	Photo1		=	frm.Photo1.value
	Photo2		=	frm.Photo2.value	
	Photo3		=	frm.Photo3.value	
	Photo1		=	frm.Photo1.value
	Name		=	frm.Name.value
	Phone		=	frm.Phone.value
	Email		=	frm.Email.value
	Password	=	frm.Password.value
	HeardFrom	=	frm.HeardFrom.value
	url			=	"reviewAd.php?Type="+Type+"&Category="+Category+"&County="+County+"&Title="+Title+"&Description="+Description+"&Price="+Price+"&Photo1="+Photo1+"&Name="+Name+"&Phone="+Phone+"&Email="+Email+"&Password="+Password+"&HeardFrom="+HeardFrom+"&Photo2="+Photo2+"&Photo3="+Photo3+""
	window.open(url,'subWnd','HEIGHT=600 width=600 scrollbars=yes menubar=no')
}






///////////GENERAL FUNCTIONS 
//#### Text area counter
function textCounter(field,cntfield,maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
cntfield.value = maxlimit - field.value.length;
}
//### Email check
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    //alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}

function showimg(num){
		for(i=1;i<=3;i++){
				if(num==i){
					document.getElementById("P"+i).style.display="block"
				}else{
					document.getElementById("P"+i).style.display="none"
				}
		}
}

function senq(frm){
		if(frm.Name.value==''){
			alert("Please enter the name")
			frm.Name.focus()
			return false
		}
		if(echeck(frm.Email.value)==false){
			alert("Please enter the proper email address.")
			frm.Email.focus()
			return false
		}
		if(frm.Comments.value==''){
			alert("Please enter the comments.")
			frm.Comments.focus()
			return false
		}		
}


function checksubs(frm){
	name	=	frm.T2.value
	email	=	frm.T3.value	
	if(name	==	'' || name === 'Name'){
		alert("Please enter the name")
		return false
	}
	if(echeck(email)==	false){
		alert("Please enter valid email address")
		return false
	}	
}


///AJAX

function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your Browser Sucks!\nIt's about time to upgrade don't you think?");
	}
}

//Our XmlHttpRequest object to get the Ajax Object
	var AjaxObj = getXmlHttpRequestObject();
function checkEmail(val){
	if(echeck(val)==false){
		return false
	}
	str	=	'?email='+val
	AjaxObj.open("GET", 'https://www.mysell.ie/eme.php'+str, true);
	AjaxObj.onreadystatechange = showmodels
	AjaxObj.send(null);
}

function showmodels(){
	if (AjaxObj.readyState == 4) {
		resp	=	AjaxObj.responseText
		//alert(resp)
		document.getElementById("emi").innerHTML	=	resp
	}
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function formUrl(frm){
	tp			=	frm.Type
	kwds		=	frm.Keywords
	cid			=	frm.Catid
	loc			=	frm.Location
	str			=	'';
	for(i=0;i<=2;i++){
		if(tp[i].checked==true){
		v=	tp[i].value	
		if(v==''){v='for-Sale-and-Wanted'}
		str+='/search/'+v
		}
	}

	catname	=	cid.options[cid.selectedIndex].text
	catid	=	cid.options[cid.selectedIndex].value
	if(catid==''){catid='0';catname='All Categories'}
	catname	=	catname.replace(/^\s+|\s+$/g, '') ;
	catname	=	catname.replace(" ","-");
	catname	=	catname.replace("&nbsp;","-");
	str+='/'+catname+'/'+catid
	
	locv	=	loc.value
	if(locv==''){locv='All-Ireland'}
	if(kwds.value== '' || kwds.value=='Enter Keywords'){Keywords='Everything';}else{Keywords=kwds.value;}
	str+='/'+locv+'/?Keywords='+Keywords

window.location.href=str
	
}