//function to validate format and area code of phone field
function isPhoneFormatValid(phoneValue){
var phoneDigits = "";
var fakeAreaCodes = "";

//compiling the list of fake area codes to validate against
fakeAreaCodes = "222,333,444,555,666,777,999,211,311,411,511,611,711,811,911";

//extracting only the digits out of the phone field
for(i=0; i < phoneValue.length; i++){
	if (isValueNumeric(phoneValue.charAt(i))){
		phoneDigits = phoneDigits + phoneValue.charAt(i);
	}
}

//extracting the area code
var areaCode = "0";
if (phoneDigits.length >= 3){
	areaCode = phoneDigits.substring(0,3);
}
if (phoneDigits.length != 10 || fakeAreaCodes.indexOf(areaCode) != -1 || parseInt(areaCode) <= 200){
	return false;
}

if (phoneDigits.length == 10){
	if (phoneDigits.indexOf("8888888888") != -1){
	return false;
	}
}

return true;

}


function isFNameValid(elemValue){
	if (elemValue.length < 2){
		return false;
	}
	return true;
}

function isLNameValid(elemValue){
	if (elemValue.length < 2){
		return false;
	}
	return true;
}

function isCompanyValid(elemValue){
	if (elemValue.length < 2){
		return false;
	}
	return true;
}


function isStreetAddressValid(elemValue){
	if (elemValue.length < 3){
		return false;
	}
	return true;
}

function isCityValid(elemValue){
	if (elemValue.length < 2){
		return false;
	}
	return true;
}

function isZipValid(elemValue){
	if (elemValue.length < 5 || elemValue.length > 5){
		return false;
	}
	
	if (isNaN(elemValue))
	 {
		return (false);
	 }
	
	return true;
}

function isEmailValid(elemValue){
	
	elemValue = elemValue.toLowerCase();
	var validRegExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    // search email text for regular exp matches
    if (!elemValue.match(validRegExp)) {
		return false;
	}
	
	if (elemValue.indexOf("test@") != -1 || elemValue.indexOf("@test") != -1){
		return false;
	}	
	return true;
}

function isEmailLengthValid(elemValue){
	if (elemValue.length < 6 || elemValue.length > 60){
		return false;
	}
	return true;

}

//function to check public domain email ids
function isPublicEmailDomain(elemVar){
	var emailPattern= 'bellsouth,yahoo,hotmail,gmail,abcd,sbcglobal,att,aol,quinstreet,comcast,cox,msn,cablevision,earthlink,verizon';
		var tokens = emailPattern.split(",");
		var emailIdStr = elemVar.value;
		for(i=0; i<tokens.length; i++){
			tokens[i]="@" + tokens[i] + ".";
			if (emailIdStr.indexOf(tokens[i])!= -1){
				var domain = emailIdStr.substring(emailIdStr.indexOf("@") + 1, emailIdStr.length);
				alert("We recommend that you enter your company email address \n and not the address of a public email provider like \' " + domain + "\'");
				return;
			}		
		}
}

//util function to check empty field
function isEmpty(elemValue){
	if (elemValue != null && elemValue != '' && elemValue != 0){
		return false;
	}else{
		return true;
	}
}

//util function to check if field is numeric
function isValueNumeric(elemValue){
	if (elemValue.search(/^[-+]?\d+(\.\d+)?$/) != -1){
		return true;
	}else{
		return false;
	}
}

//function to validate all common form fields
//document.frmreq.firstname,document.frmreq.lastname,document.frmreq.phone,document.frmreq.city,document.frmreq.state,document.frmreq.zip,document.frmreq.email
function validateAllCommonFormFields(firstname,lastname,phone,city,state,zip,company,email){

	if (isEmpty(firstname.value)){
		alert('Please Enter your First Name');
		firstname.focus();
		return false;
	}
	
	if (isEmpty(lastname.value)){
		alert('Please Enter your Last Name');
		lastname.focus();
		return false;
	}
	
		if (isEmpty(phone.value)){
		alert('Please Enter the Phone Number');
		phone.focus();
		return false;
	}

		
	if (isEmpty(city.value)){
		alert('Please Enter the city');
		city.focus();
		return false;
	}
	
	if (isEmpty(state.value)){
		alert('Please Enter the State');
		state.focus();
		return false;
	}
	
	if (isEmpty(zip.value)){
		alert('Please Enter the Zip');
		zip.focus();
		return false;
	}
	
	
	if (isEmpty(company.value)){
		alert('Please Enter your Company Name');
		company.focus();
		return false;
	}
	

	
	if (isEmpty(email.value)){
		alert('Please Enter the Email ID');
		email.focus();
		return false;
	}
	
		
	if (!isFNameValid(firstname.value)){
		alert('First Name should have minimum 2 characters');
		firstname.focus();
		return false;
	}
	
	if (!isLNameValid(lastname.value)){
		alert('Last Name should have minimum 2 characters');
		lastname.focus();
		return false;
	}
	
	if (!isCompanyValid(company.value)){
		alert('Company Name should have minimum 2 characters');
		company.focus();
		return false;
	}
	
	
	
	if (!isCityValid(city.value)){
		alert('City should have minimum 2 characters');
		city.focus();
		return false;
	}
	
	if (!isZipValid(zip.value)){
		alert('Please Enter correct Zip code');
		zip.focus();
		return false;
	}
	
	if (!isPhoneFormatValid(phone.value)){
		alert('Please provide valid phone number');
		phone.focus();
		return false;
	}
	
	if (!isEmailValid(email.value)){
		alert('Please Enter valid Email Id');
		email.focus();
		return false;
	}
	
	if (!isEmailLengthValid(email.value)){
		alert('Email Id should have 6-60 characters');
		email.focus();
		return false;
	}


	document.frmreq.action="landing_page_post.php";
	document.frmreq.method="POST";
	document.frmreq.submit();
	
	return true;
}

/* function to validate website name on SEO forms */
function validateSeoWebsitefield(seoWebsiteStr){
	var domainExtstr = "ac,ad,ae,aero,af,ag,ai,al,am,an,ao,aq,ar,arpa,as,asia,at,au,aw,ax,az,ba,bb,bd,be,bf,bg,bh,bi,biz,bj,bm,bn,bo,br,bs,bt,bv,bw,by,bz,ca,cat,cc,cd,cf,cg,ch,ci,ck,cl,cm,cn,co,com,coop,cr,cu,cv,cx,cy,cz,de,dj,dk,dm,do,dz,ec,edu,ee,eg,er,es,et,eu,fi,fj,fk,fm,fo,fr,ga,gb,gd,ge,gf,gg,gh,gi,gl,gm,gn,gov,gp,gq,gr,gs,gt,gu,gw,gy,hk,hm,hn,hr,ht,hu,id,ie,il,im,in,info,int,io,iq,ir,is,it,je,jm,jo,jobs,jp,ke,kg,kh,ki,km,kn,kp,kr,kw,ky,kz,la,lb,lc,li,lk,lr,ls,lt,lu,lv,ly,ma,mc,md,me,mg,mh,mil,mk,ml,mm,mn,mo,mobi,mp,mq,mr,ms,mt,mu,museum,mv,mw,mx,my,mz,na,name,nc,ne,net,nf,ng,ni,nl,no,np,nr,nu,nz,om,org,pa,pe,pf,pg,ph,pk,pl,pm,pn,pr,pro,ps,pt,pw,py,qa,re,ro,rs,ru,rw,sa,sb,sc,sd,se,sg,sh,si,sj,sk,sl,sm,sn,so,sr,st,su,sv,sy,sz,tc,td,tel,tf,tg,th,tj,tk,tl,tm,tn,to,tp,tr,travel,tt,tv,tw,tz,ua,ug,uk,us,uy,uz,va,vc,ve,vg,vi,vn,vu,wf,ws,xn--0zwm56d,xn--11b5bs3a9aj6g,xn--80akhbyknj4f,xn--9t4b11yi5a,xn--deba0ad,xn--g6w251d,xn--hgbk6aj7f53bba,xn--hlcj6aya9esc7a,xn--jxalpdlp,xn--kgbechtv,xn--zckzah,ye,yt,yu,za,zm,zw";
	//trim the string
	seoWebsiteStr = seoWebsiteStr.toLowerCase();
	seoWebsiteStr = seoWebsiteStr.replace(/^\s+|\s+$/g,"");
	seoWebsiteStr = seoWebsiteStr + "/"
	if (seoWebsiteStr.indexOf(" ") != -1 || seoWebsiteStr.indexOf(".") < 0){
		alert('Please enter a valid website name. e.g. www.websitename.com');
		return false;
	}else{
		 var domainExtstrSArr = domainExtstr.split(",");
		for (i=0;i < domainExtstrSArr.length;i++){
			if(seoWebsiteStr.indexOf("." + domainExtstrSArr[i] + "/") != -1){
			return true;			
			}
			
		}
	}
	alert('Please enter a valid website name. e.g. www.websitename.com');
	return false;
}

function validateServiceQs(elemObj){
	//service Qs validation
	var flag = true;
	var formObj = elemObj.form;
	var cat_id = "";
	if (formObj.id !=null){
		cat_id = formObj.id.value;
	}else if(formObj.category_id != null){
		cat_id = formObj.category_id.value;
	}

	//validate website Q for SEO
	if (cat_id != "" && cat_id == "120" && formObj.ans2 != null){
		if(!validateSeoWebsitefield(formObj.ans2.value)){
			formObj.ans2.focus();
			flag = false;
		}
	}
	
	return flag;
}