var reEmail = /^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/;

String.prototype.trim = function(){
    return this.replace(/^\s+/,'').replace(/\s+$/,'');            
}

var blockAllowStr = "talkpoint.com,onstreammedia.com,webex.com,raindance.com,brainshark.com,macromedia.com,netbriefings.com,communicast.com,placeware.com,unisfair.com,ilinc.com,microsoft.com,accelacommunications.com,vcall.com,ivtweb.com,reflectsystems.com,livemeeting.com,brighttalk.com,streamlogics.com,vdat.com,vdat.de|";

function isblank(s)
{
	for(var i = 0; i < s.length; i++)	{
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\t')) return false;
	}
	return true;
}

function isNumeric(sText)
{
	var ValidChars = "0123456789.";
	var Char;
	
	
	for (i = 0; i < sText.length; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
			return false;
	}
	return true;

}

function validatenone(ctrl,isrequired){	
	ctrl.value=ctrl.value.trim();	
	if (ctrl.value=="")
		if (isrequired){ 
			alert("This is a required field");
			ctrl.focus();
			return false;
		}	
	return true;	
}

function validatetextbox(ctrl,isrequired){	
	return validatenone(ctrl,isrequired);	
}

function validatetextarea(ctrl,isrequired){	
	return validatenone(ctrl,isrequired);
}

function validatenumeric(ctrl,isrequired){	
	if(!validatenone(ctrl,isreq)) return false;
	
	var numberstr=ctrl.value;
	if (! isNumeric(numberstr)){
			alert("Please enter a numeric value in this field"); 
			ctrl.focus();
			return false;
	}	
	return true;	
}

function validateemail(ctrl,isreq){ 	
	if(!validatenone(ctrl,isreq)) return false;
	var estr=ctrl.value.toLowerCase();
	if(!estr) return false;
	if (!isEmail(estr)){
		alert("Please enter a valid email address in this field"); 
		ctrl.focus();
		return false;
	}
	
	return true;	
}

function validatephone(ctrl,isrequired)
{ 		
	if(!validatenone(ctrl,isrequired)) return false;
	var phonestr=ctrl.value;
	if(!phonestr) return false;
	phonestr=phonestr.replace(/[^\d]*/gi,"");
	if (!phonestr || phonestr.length<7){
			alert("Please enter a valid phone number in this field"); 
			ctrl.focus();
			return false;
	}	
	if (phonestr.length==7)
		ctrl.value=(phonestr.substring(0,3)+"-"+phonestr.substring(3,7))
	else if	(phonestr.length==10)
		ctrl.value="("+phonestr.substring(0,3)+") "+phonestr.substring(3,6)+"-"+phonestr.substring(6,10);
	return true;
}

function validatelength(ctrl,maximumlength)
{ 
	ctrl.value=ctrl.value.trim();
	var phonestr=ctrl.value;
	phonestr=phonestr.replace(/[^\d]*/gi,"");
	if (ctrl.value.length>maximumlength){
			alert("Maximum allowable length ("+maximumlength+") exceeded. Please modify the information in this field and try again)"); 
			ctrl.focus();
			return false;
	}	
	return true;
}

function validateoption(ctrl,isrequired){
	if (isrequired){ 
		if(!optionselected(ctrl)){
			alert("This is a required field");
			if(ctrl.length+""=="undefined") ctrl.focus();
			else ctrl[0].focus();
			return false;
		}
	}	
	return true;
}

function validatemulticheck(ctrl,targetctrl, isrequired){
	var checkboxvalue=getcheckboxvalue(ctrl);
	targetctrl.value=checkboxvalue;
	if (isrequired){ 
		if(checkboxvalue==""){
			alert("This is a required field");
			if(ctrl.length+""=="undefined") ctrl.focus();
			else ctrl[0].focus();
			return false;
		}
	}	
	return true;
}

function validatelistbox(ctrl,isrequired,hasDefaultValue){	
	if (isrequired && ctrl.length>1){ 
		if(ctrl.selectedIndex>=0 && ctrl.options[ctrl.selectedIndex].value==""){
			alert("This is a required field");
			ctrl.focus();
			return false;
		}
	}	
	return true;
}

function validatecheckbox(ctrl,isrequired, defaultmessage){
	if (isrequired){ 
		if(!ctrl.checked){
			if(defaultmessage) alert(defaultmessage)
			else alert("Please check the box to continue");
			ctrl.focus();
			return false;
		}
	}	
	return true;
}


function optionselected(ctrl){
	if(ctrl.length+""=="undefined") {
		return ctrl.checked;
	}
	else {
		for(var i=0;i<ctrl.length;++i){
			if(ctrl[i].checked) return true;
		}
		return false;
	}
}

function getcheckboxvalue(ctrl){
	var returnVal="";
	if(ctrl.length+""=="undefined") {
		if(ctrl.checked) returnVal=ctrl.value;
	}	
	else {
		for(var i=0;i<ctrl.length;++i){
			if(ctrl[i].checked) returnVal+=ctrl[i].value + "|";
		}
		if(returnVal!="") returnVal=returnVal.substring(0,returnVal.length-1);
	}
	return returnVal;
}

function isEmail(str) {
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  else		
	return (reEmail.test(str));
}

function registerUser(){
	with(document.regForm){
		if(!validatetextbox(firstname, true)) return false;
		if(!validatetextbox(lastname, true)) return false;
		if(!validateemail(email, true)) return false;
		if(isBlockedEmail(email.value, blockAllowStr)){
			alert("Insufficient data provided. Please try again.");
			return false;
		}
                if(!validatelistbox(std1, true)) return false;
		if(!validatetextbox(password, true)) return false;		
		if(!validatetextbox(company, true)) return false;
		if(!validatelistbox(companysize, true)) return false;
		//if(!validatetextbox(jobtitle, true)) return false;
		if(!validatelistbox(std5, true)) return false;
		if(!validatelistbox(jobfunction, true)) return false;
		if(!validatephone(workphone, true)) return false;
		if(!validatelistbox(country, true)) return false;
		if(!validatelistbox(companyindustry, true)) return false;
		if(!validatelistbox(std4, true)) return false;
    		if(document.regForm.std3 && !validatecheckbox(std3, true, "Please check the Terms of Use and Privacy Policy to continue")) return false;
		playerpref.value=std4.options[std4.selectedIndex].value=="Windows Media"?"wm":"rm";
		//this part is for update. If the email address is changed, reset the confirm flag.
		if(document.regForm.originalemail){
			if(email.value != originalemail.value){
				std10.value = "N";
			}
		}
		if(document.regForm.partnerref 
				&& !document.regForm.partnerref.value // no value is passed
				&& location.href.indexOf("/webcasts/registration")!=-1){ //insight24 reg page
			document.regForm.partnerref.value = "website"
		}
		submit();
	}
}

function loginUser(){
	with(document.loginForm){
		if(!validateemail(email, true)) return false;
		if(isBlockedEmail(email.value, blockAllowStr)){
			alert("Insufficient data provided. Please try again.");
			return false;
		}
		if(!validatetextbox(password, true)) return false;
		submit();
	}
}

function launchForgotPassword(){
	window.open("/eventRegistration/prereg/forgotPassword.jsp?clientid=600&submitbutton=/view/insight24/img/button_submit_on.gif&cancelbutton=/view/insight24/img/button-cancel-on.gif&donebutton=/view/insight24/img/button-done-on.gif","ForgotPassword","scrollbars=no,width=320,height=250");
			return false;
}

function isBlockedEmail(email,blockAllowStr){
	if(blockAllowStr=="" || blockAllowStr=="|") return false;
	email=email.toLowerCase();
	blockAllowStr=blockAllowStr.toLowerCase();
	var blockAllowArr=blockAllowStr.split("|");
	var userdomain=email.substring(email.indexOf("@")+1);
	if(blockAllowArr[0]!="" && blockAllowArr[1]==""){
		//has only block email list
		var blockArr=blockAllowArr[0].split(",");
		var hasSameDomain=false;
		for(var i=0;i<blockArr.length;i++){
			if(email==blockArr[i]) return true;
			else if(userdomain==blockArr[i]) hasSameDomain=true;
		}
		return hasSameDomain;
	} else if(blockAllowArr[0]=="" && blockAllowArr[1]!=""){
		//has only allow email list
		var allowArr=blockAllowArr[1].split(",");
		for(var i=0;i<allowArr.length;i++){
			if(userdomain==allowArr[i]) return false;
			else if(email==allowArr[i]) {return false;break;}
		}
		return true;
	}else if(blockAllowArr[0]!="" && blockAllowArr[1]!=""){
		//has both block and allow email list
		var blockArr=blockAllowArr[0].split(",");
		var hasSameDomain=false;
		for(var i=0;i<blockArr.length;i++){
			if(email==blockArr[i]) return true;
			else if(userdomain==blockArr[i]) hasSameDomain=true;
		}
		if(hasSameDomain){
			var allowArr=blockAllowArr[1].split(",");
			for(var i=0;i<allowArr.length;i++){
				if(email==allowArr[i]) {return false;break;}
			}
			return true;
		}
	}
	return false;
}
