var vcp_home = "vcp.php";
function fetch_object(idname) {
	if (document.getElementById)
		obj = document.getElementById(idname);
	else if (document.all)			
		obj = document.all[idname];			
	else if (document.layers)
		obj = document.layers[idname];			
	return obj;
}
function validEmail(field) {
	var goodEmail = field.value.match(/\b(^(\S+@).+((\.biz)|(\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
	if (!goodEmail){
	   alert("Please enter a valid e-mail address.");
	   field.focus();
	   field.select();
	   return false;
	  }
	return true;
}
function checkLogin() {
	if(fetch_object("login").username.value=="") {
		alert(" Please enter your user name.  ");
		fetch_object("login").username.focus();
		return false;
	}
	if(fetch_object("login").password.value=="") {
		alert(" Please enter the password.  ");
		fetch_object("login").password.focus();
		return false;
	}
	
	xmlhttpPost('merHandler.php','login','error_login');
}
function exAccount() {
	if(fetch_object("existAccount").checked == true) {
		fetch_object("loginForm").style.display="block";
		fetch_object("registerForm").style.display="none";
		fetch_object("learnMore").style.display="none";
		fetch_object("login").username.focus();
	}
	if(fetch_object("newAccount").checked == true) {
		fetch_object("registerForm").style.display="block";
		fetch_object("loginForm").style.display="none";
		fetch_object("learnMore").style.display="none";
		fetch_object("register").username.focus();
	}
	if(fetch_object("toLearnMore").checked == true) {
		fetch_object("learnMore").style.display="block";
		fetch_object("registerForm").style.display="none";
		fetch_object("loginForm").style.display="none";		
		closeAllTopic();
	}	
}
function checkRegister() {
	if(fetch_object("register").username.value=="") {
		alert(" Please enter your name.  ");
		fetch_object("register").username.focus();
		return false;
	}
	
	if(fetch_object("register").passwordA.value=="") {
		alert(" Please enter the password.  ");
		fetch_object("register").passwordA.focus();
		return false;
	}
	if(fetch_object("register").passwordA.value.length<4) {
		alert(" The length of password should not be less than 4.  ");
		fetch_object("register").passwordA.value="";
		fetch_object("register").passwordA.focus();
		return false;
	}	
	if(fetch_object("register").passwordB.value=="") {
		alert(" Please verify the password.  ");
		fetch_object("register").passwordB.focus();
		return false;
	}	
	if(fetch_object("register").passwordA.value!=fetch_object("register").passwordB.value) {
		alert(" The passwords do not match.  ");
		fetch_object("register").passwordB.value="";
		fetch_object("register").passwordB.focus();
		return false;
	}
	
	if(!validEmail(fetch_object("register").email)) return false;
	
	if(fetch_object("register").city.value=="") {
		alert(" Please enter the city field .  ");
		fetch_object("register").city.focus();
		return false;
	}	
	if(fetch_object("register").province.value=="") {
		alert(" Please select a province .  ");
		fetch_object("register").province.focus();
		return false;
	}
	if(fetch_object("register").gender.value=="") {
		alert(" Gender is a required field.  ");
		fetch_object("register").gender.focus();
		return false;
	}	
	if(fetch_object("register").req_check.checked!=true) {
		alert(" You have to agree with the terms .  ");
		fetch_object("register").req_check.focus();
		return false;
	}	
	
	xmlhttpPost('merHandler.php','register','error_register');
}

function xmlhttpPost(strURL,formID,dispID) {
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
		//self.xmlHttpReq.overrideMimeType("text/xml");
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open("POST", strURL, true);
	self.xmlHttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	self.xmlHttpReq.onreadystatechange = function() {
		if (self.xmlHttpReq.readyState == 4) {
			updatepage(self.xmlHttpReq.responseText,dispID);
		}
	}
	self.xmlHttpReq.send(getquerystring(formID));
}

function getquerystring(formID) {
	var qstr= "";
	obj = document.getElementById(formID);
	for(i=0;i<obj.length;i++) {
		qstr+=obj[i].name+"="+escape(obj[i].value)+"&";
	}
	return qstr;
}
function updatepage(str,dispID){
	if(str=="101") {
		document.getElementById(dispID).innerHTML = "Wrong username or password, please try again";
		document.getElementById("login").password.value = '';
		fetch_object("loginForm").style.display="block";
		fetch_object("registerForm").style.display="none";
		fetch_object("login").username.focus();
	}
	else if(str=="102") {
		window.location = "vcp.php";
	}
	else if(str=="103") {
		document.getElementById(dispID).innerHTML = "User name or email has been taken. Try again.";
		fetch_object("registerForm").style.display="block";
		fetch_object("loginForm").style.display="none";
		fetch_object("register").username.focus();
	}
	else if(str=="104") {
		alert("The user name is available.");
		fetch_object("register").passwordA.focus();
		fetch_object("register").todo.value="REG";		
	}	
	else if(str=="105") {
		alert("The user name is NOT available.");
		fetch_object("register").username.select();
		fetch_object("register").username.focus();
		fetch_object("register").todo.value="REG";	
	}
	else if(str=="106") {
		alert("The email address is available.");
		fetch_object("register").city.focus();
		fetch_object("register").todo.value="REG";		
	}	
	else if(str=="107") {
		alert("The email address is NOT available.\nPlease use another one.");
		fetch_object("register").email.select();
		fetch_object("register").email.focus();
		fetch_object("register").todo.value="REG";	
	}	
	else if(str=="108") {
		window.location = "vcp.php";
	}
	else if(str=="109") {
		document.getElementById('config').disabled=true;
		document.getElementById('config').style.color='black';
		document.getElementById('config').blur();
		document.getElementById('config').style.backgroundColor='white';
		document.getElementById('config_button').style.display='none';
		alert("Changes have been saved.");
	}	
	else if(str=="110") {
		window.location = vcp_home+"?t=s1";
	}	
	else if(str=="111") {
		window.location = vcp_home+"?t=s3";

	}	
	else {
		document.getElementById(dispID).innerHTML = str;
	}
}
function checkAvailability(what) {
	if(what=="email") {
		if(!validEmail(fetch_object("register").email)) 
			return false;
	}
	else if(what=="username") {
		if(fetch_object("register").username.value=="") {
			alert("User name can't be empty.");
			return false;
		}
	}
	fetch_object("register").todo.value=what;
	xmlhttpPost('merHandler.php','register','error_register');
}

PN_help = function(linkRef, options) {
  var DEFAULT_POPUP_TARGET = 'PN_popup';
  var DEFAULT_POPUP_HEIGHT = 500;
  var DEFAULT_POPUP_WIDTH = 690;

  if (!window.focus) return true;

  var default_options = {
    width: DEFAULT_POPUP_WIDTH,
    height: DEFAULT_POPUP_HEIGHT,
    toolbar: 1,
    scrollbars: 1,
    location: 1,
    statusbar: 1,
    menubar: 1,
    resizable: 1
  };

  options = options || {};
  for (var option in default_options) {
    if ((! (option in options)) || options[option] === null) {
      options[option] = default_options[option];
    }
  }

  var href = typeof linkRef == 'string' ? linkRef : linkRef.href;
  var target = options.target || linkRef.target || DEFAULT_POPUP_TARGET;
  delete options['target'];

  var optionString = '';
  for (var option in options) {
    if (optionString != '') optionString += ',';
    optionString += option + '=' +
      (options[option] === true ? '1' :
         (options[option] === false ? '0' : options[option]));
  }

  var newWin = window.open(href, target, optionString);
  newWin.focus();
  
  return false;
}
function autoTab(last,next) {
	if(fetch_object(last).value.length==fetch_object(last).maxLength) {
		if(isNaN(fetch_object(last).value)) {
			alert("Please enter valid value for this field.");
			fetch_object(last).focus();
			fetch_object(last).select();
		}
		else {
			fetch_object(next).focus();
			fetch_object(next).select();
		}	
	}		
}
function checkBusForm() {
	if(fetch_object("busForm").VName.value=="") {
		alert(" Please enter the Business Name.  ");
		fetch_object("busForm").VName.focus();
		return false;
	}
	if(fetch_object("busForm").website.value=="") {
		alert(" Website is a required field.");
		fetch_object("busForm").website.focus();
		return false;
	}
	if(fetch_object("busForm").contact_name.value=="") {
		alert(" Please enter your contact name.");
		fetch_object("busForm").contact_name.focus();
		return false;
	}	

	if(!validEmail(fetch_object("busForm").email)) return false;	
	if(fetch_object("busForm").phone.value=="") {
		alert(" Please enter your phone number.");
		fetch_object("busForm").phone.focus();
		return false;
	}	
}
function menuTog(menuID) {
	if(document.getElementById(menuID).style.display=='block')
		document.getElementById(menuID).style.display='none';
	else 
		document.getElementById(menuID).style.display='block';
}
function learnToggle(tid) {
	var text = new Array();
	text[1]='How it works?';
	text[2]='Who sees your deal?';
	text[3]='Costs & Payments';
	if(document.getElementById("topic_"+tid).style.display=='none') {
		document.getElementById("topic_"+tid).style.display='block';
		document.getElementById("topic_line_"+tid).innerHTML='- '+text[tid];
	}
	else {
		document.getElementById("topic_"+tid).style.display='none';
		document.getElementById("topic_line_"+tid).innerHTML='+ '+text[tid];			
	}	
}
function startnow() {
	fetch_object("registerForm").style.display="block";
	fetch_object("loginForm").style.display="none";
	fetch_object("learnMore").style.display="none";
	fetch_object("register").username.focus();
	fetch_object("toLearnMore").checked = false;
	fetch_object("newAccount").checked = true;
	fetch_object("existAccount").checked = false;
}
function closeAllTopic() {
	var text = new Array();
	text[1]='How it works?';
	text[2]='Who sees your deal?';
	text[3]='Costs & Payments';
	document.getElementById("topic_1").style.display='none';
	document.getElementById("topic_line_1").innerHTML='+ '+text[1];	
	document.getElementById("topic_2").style.display='none';
	document.getElementById("topic_line_2").innerHTML='+ '+text[2];	
	document.getElementById("topic_3").style.display='none';
	document.getElementById("topic_line_3").innerHTML='+ '+text[3];			
}