//functions used on most pages javascript.
//John Derek Carlson: 4/2/04

function HandleClick (val){
			if ((c = val.indexOf("model")) != -1){
				location.href = ""+http_host+"/viewmodel.php?id=" + val.substring(0, c);
			}
			else if ((c = val.indexOf("photographer")) != -1){
				location.href = ""+http_host+"/viewphotographer.php?id=" + val.substring(0, c);
			}
			else if ((c = val.indexOf("fashion_des")) != -1){
				location.href=""+http_host+"/viewfashion_des.php?id=" + val.substring(0, c);
			}
			else if ((c = val.indexOf("make_up")) != -1){
				location.href=""+http_host+"/viewmake_up.php?id=" + val.substring(0, c);
			}
			else location.href=""+http_host+"/viewportfolio.php?id=" + val;
		}
function openMessageWindow(action, id, type){
	/*
	var messWin;
	
	if (action == "View")
		messWin = window.open('send_message.php?rec_id='+id, null, 'location=no,scrollbars=yes,width=720,height=300');
	else
		messWin = window.open('send_message.php?rec_id='+id, null, 'scrollbars=yes,width=720,height=300,location=no,menubar=no,status=no');
		
	messWin.focus();
	*/
	
	if (action == "View")
		Window.location = 'send_message.php?rec_id='+id, null, 'location=no,scrollbars=yes';
	else
		Window.location = 'send_message.php?rec_id='+id, null, 'scrollbars=yes,location=no';
		

}

function ValidateForm_Register(){
	var ok = true;
	var mess = '';
	if ((!document.memberInfo.type[0].checked) && (!document.memberInfo.type[1].checked) && (!document.memberInfo.type[2].checked) && (!document.memberInfo.type[3].checked) && (!document.memberInfo.type[4].checked)){
		mess = "You must select a membership type.";
		ok = false;
	}
	//if (!document.memberInfo.state.value.match("/[a-z]/i")){
	//	document.memberInfo.state.className ="missing";
	//	ok = false;
	//} else document.memberInfo.state.className = "ok";
	if (document.memberInfo.zip.value == ''){
		document.memberInfo.zip.className = "missing";
		ok = false;
	} else document.memberInfo.zip.className = "ok";
	if (document.memberInfo.Fname.value == ''){
		document.memberInfo.Fname.className = "missing";
		ok = false;
	} else document.memberInfo.Fname.className = "ok";
	if (document.memberInfo.Lname.value == ''){
		document.memberInfo.Lname.className="missing";
		ok = false;
	} else document.memberInfo.Lname.className="ok";
	if (document.memberInfo.email.value == ''){
		document.memberInfo.email.className ="missing";
		ok = false;
	} else document.memberInfo.email.className="ok";
	if (document.memberInfo.c_email.value == ''){
		document.memberInfo.c_email.className ="missing";
		ok = false;
	} else document.memberInfo.c_email.className = "ok";
	if (document.memberInfo.city.value ==''){
		document.memberInfo.city.className="missing";
		ok = false;
	} else document.memberInfo.city.className ="ok";
	if (document.memberInfo.state.value == ''){
		document.memberInfo.state.className="missing";
		ok = false;
	} else document.memberInfo.state.className= "ok";
	if (document.memberInfo.state.value.length != 2){
		document.memberInfo.state.className ="missing";
		ok = false;
	} else document.memberInfo.state.className = "ok";
	/*if (!validateEmail(document.memberInfo.email.value)){
		document.memberInfo.email.className = "missing";
		ok = false;
	} else document.memberInfo.email.className = "ok";*/
	if (!isDate(document.memberInfo.birthdate.value)){
		document.memberInfo.birthdate.className = "missing";
		ok = false;
	} else document.memberInfo.birthdate.className = "ok";
	if (document.memberInfo.gender.value == ''){
		document.memberInfo.gender.className = "missing";
		ok = false;
	} else document.memberInfo.gender.className = "ok";
	if (document.memberInfo.email.value != document.memberInfo.c_email.value){
		alert ("Email address must be entered if both fields exactly the same.");
		ok = false;
	}
	if (document.memberInfo.agree.checked == false){
		alert ("You must accept the terms of use and the privacy policy.");
		ok = false;
	}
	if (document.memberInfo.linkBack.checked == false) {
		alert ("You agree to include a link to Americanmodel.com on your website.");
		ok = false;
	}
	if (ok == false)
		alert (mess + "\nSome required information is missing or incorrect.");
	else 
		document.memberInfo.submit();
}

function validateEmail( strValue) {
/************************************************
DESCRIPTION: Validates that a string contains a
  valid email pattern.

 PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS: Accounts for email with country appended
  does not validate that email contains valid URL
  type (.com, .gov, etc.) or valid country suffix.
*************************************************/
var objRegExp  ="/^.+@.+\..{2,3}$/; ";

  //check for valid email
  return strValue.match(objRegExp);
} 

function DoSort(page, sort){
	var url;
	url = page + "?sortBy=";
	url += sort.substring(0, sort.indexOf(" "));
	url += "&sortOrder=";
	url += sort.substring(sort.indexOf(" ") + 1, sort.length);
	location.href=url;    		
}

function ValidateForm_Manage(type){
	var ok;
	var c;
	var test;
	var Chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var nums = "0123456789";
	ok = true;
		
	if (document.update.Fname.value ==''){
		document.update.Fname.className="missing";
		ok = false;
	}
	else
		document.update.Fname.className="ok";
		
	if (document.update.Lname.value ==''){
		document.update.Lname.className="missing";
		ok = false;
	}
	else
		document.update.Lname.className="ok";
		
	if (document.update.birthdate.value =='' || (!isDate(document.update.birthdate.value))){
		document.update.birthdate.className="missing";
		ok = false;
	}
	else
		document.update.birthdate.className="ok";
		
	if (document.update.city.value ==''){
		document.update.city.className="missing";
		ok = false;
	}
	else
		document.update.city.className="ok";
		
	if (document.update.zip.value == ''){
		document.update.zip.className = "missing";
		ok = false;
	} else document.update.zip.className = "ok";
	
	if (document.update.state.value.length != 2){
		document.update.state.className = "missing";
		ok = false;
	} else document.update.state.className = "ok";
	for (c=0; c < document.update.state.value.length; c++){
		if (Chars.indexOf(document.update.state.value.substr(c,1)) == -1){
			document.update.state.className = "missing";
			ok = false;
			break;
		}
	}
	//validation for members with premium service only.							
 	if (type == "member"){								
		if (document.update.industry.value == ''){
			document.update.industry.className="missing";
			ok = false;
		}
		else
			document.update.industry.className="ok";
			
		if (document.update.job.value == ''){
			document.update.job.className="missing";
			ok = false;
		}
		else 
			document.update.job.className="ok";
	}	
	if (document.update.phoneNum.value != ''){
		if (document.update.phoneNum.value.length != 10){
			document.update.phoneNum.className = "missing";
			ok = false;
		}
		else {
			for (c=0; c<document.update.phoneNum.value.length; c++){
				test = document.update.phoneNum.value.charAt(c);
				if (nums.indexOf(test) == -1){
					document.update.phoneNum.className = "missing";
					ok = false;
					break;
				}
				else document.update.phoneNum.className = "ok";
			}
		}
	}
			
	if (ok == false){
		alert ("Some required information is missing or incorrect.");
		return false;
	}
	if (document.update.newPass.value != document.update.confirmPass.value){
		alert ("You must enter your new password exactly the same in both fields.");
		return false;
	}
	document.update.submit();
}
function changeService(){
	location.href=""+http_host+"/manage.php?service=" + document.update.service.value;
}

function ValidateForm_steptwo(type){
	var ok;
	var c;
	var test;
	var Chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var nums = "0123456789";
	ok = true;
	
	//added 4/20
	if (document.update.state.value.length != 2){
		document.update.state.className ="missing";
		ok = false;
	} else document.update.state.className = "ok";
	for (c = 0; c < document.update.state.value.length; c++){
		if (Chars.indexOf(document.update.state.value.substr(c, 1)) == -1){
			document.update.state.className = "missing";
			ok = false;
			break;
		}
	}
	if (document.update.newPass.value == ''){
		document.update.newPass.className="missing";
		ok = false;
	}
	else
		document.update.newPass.className="ok";
		
	if (document.update.confirmPass.value == ''){
		document.update.confirmPass.className="missing";
		ok = false;
	}//check if the passwords match at the end
	else
		document.update.confirmPass.className="ok";
	if (document.update.Fname.value ==''){
		document.update.Fname.className="missing";
		ok = false;
	}
	else
		document.update.Fname.className="ok";
		
	if (document.update.Lname.value ==''){
		document.update.Lname.className="missing";
		ok = false;
	}
	else
		document.update.Lname.className="ok";
		
	if (document.update.birthdate.value =='' || (!isDate(document.update.birthdate.value))){
		document.update.birthdate.className="missing";
		ok = false;
	}
	else
		document.update.birthdate.className="ok";
		
	if (document.update.city.value ==''){
		document.update.city.className="missing";
		ok = false;
	}
	else
		document.update.city.className="ok";
	if (document.update.zip.value == ''){
		document.update.zip.className="missing";
		ok = false;
	}
	else document.update.zip.className = "ok";
//validation for members with premium service only.							
	if (type == "member"){										
		if (document.update.industry.value == ''){
			document.update.industry.className="missing";
			ok = false;
		}
		else
			document.update.industry.className="ok";
			
		if (document.update.job.value == ''){
			document.update.job.className="missing";
			ok = false;
		}
		else 
			document.update.job.className="ok";
	}
	if (type != "member"){
		if (document.update.bio.value == ''){
		document.update.bio.className="missing";
		ok = false;
		}
		else document.update.bio.className = "ok";
		/*if (document.update.phoneNum.value != ''){
			if (document.update.phoneNum.value.length != 10){
				document.update.phoneNum.className = "missing";
				ok = false;
			}
			else {
				for (c = 0; c < document.update.phoneNum.value.length; c++){
					if (nums.indexOf(document.update.phoneNum.value.substring(c, 1)) == -1){
						document.update.phoneNum.className = "missing";
						ok = false;
					}
				}
			}
		} else document.update.phoneNum.className = "ok";*/
	}						
	if (ok == false){
		alert ("Some required information is missing or incorrect.");
		return false;
	}
	if (document.update.newPass.value != document.update.confirmPass.value){
		alert ("You must enter your new password exactly the same in both fields.");
		return false;
	}
	document.update.submit();
}

function getCookie(NameOfCookie){
	if (document.cookie.length > 0){
		begin = document.cookie.indexOf(NameOfCookie+"=");
		if (begin != -1){
			begin += NameOfCookie.length+1;
			end = document.cookie.indexOf(";", begin);
			if (end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(begin, end)); 
		}
	}
	return null;
}



function setCookie(NameOfCookie, value, expiredays){
	var ExpireDate = new Date ();
	var expr;
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
	if (expiredays == null)
		expr = '';
	else
		expr = "; expires=" + ExpireDate.toGMTString();
	document.cookie = NameOfCookie + "=" + escape(value) + expr;
}



function delCookie (NameOfCookie){
	if (getCookie(NameOfCookie)) {
		document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function openContactWindow(id, type){
	var contactWin;
	contactWin = window.open('contact.php?id=' + id + "&type=" + type, null, 'location=no,scrollbars=yes,width=500,height=300');
	contactWin.focus();
}