// JavaScript Document

function setFldValue(cmd,fld)
{
	defval = new Array();
	defval["company"] = "Company Name (optional)";
	//defval["street"] = "Street Address";
	//defval["apartment_info"] = "Apt, Suite, Bldg. (Optional)";
	//defval["address"] = "Street Address";
	//defval["city"] = "Town/City";
	//defval["country"] = "";
	//defval["stateprovince"] = "Province";
	//defval["postalzip"] = "Zip / Postal Code";
	defval["card_number"] = "Credit Card Number";
	defval["card_sec_code"] = "Sec. Code";
	//defval["card_exp_month"] = "";
	//defval["card_exp_year"] = "";
	obj = document.getElementById(fld);
	if(cmd == "clear" && obj.value == defval[fld])
		obj.value = "";
	else if(cmd == "set" && obj.value == "")
		obj.value = defval[fld];
}

function renew_account()
{
	yn = confirm('Do you want to login to renew your account');
	if(yn) login();
	return false;
}

function signup_form(cmd,id)
{
	var valid;
	var flds = "fname,lname,email,confemail,password,cpassword,country,newsletter,activation_code".split(",");
	//if(getObValue("mode") == "activate")
	
	valid = validate_form(document.form,flds);
	if(
		valid && document.getElementById("password_msg").innerHTML == "" 
		&& document.getElementById("cpassword_msg").innerHTML == ""
		&& document.getElementById("email_msg").innerHTML == ""
		&& document.getElementById("confemail_msg").innerHTML == ""

	) 
	{
		if(getObjValue("discountcode") != "")
		{
			total = getObjValue("users") * getObjValue("price");		
			ajax_search(document.form,"","membership_controller2.php?section=discount&returntype=boolean&promo=" + getObjValue("discountcode") + "&amount=" + total,false,"getAjaxResponse('discount');");	
			//return false;
		}
		else					
			return true;
	}
	return false;
}

function getAjaxResponse(cmd)
{
	if(cmd == "discount")
	{
		document.getElementById("pcodemsg").innerHTML = ajaxResponse;
		//alert(ajaxResponse);
		if(isNaN(ajaxResponse))
		{
			//return false;
			setObjValue("discountcode","");
		}
		//else
		document.form.submit();
	}
}

function signupByActivation()
{
	alert(ajaxResponse);
}

function checkEmail(email)
{
	var obj = document.getElementById(email);
	if(obj.value != "")
	{
		thisurl = "membership_controller2.php?section=other&cmd=checkemail&email=" + obj.value;
		ajax_search(document.form,email + "_msg",thisurl,false);
		//newWindow(thisurl + "&sid=99" ,"test",550,550,'yes');
	}
	else
		setObjValue(email + "_msg","");
}

function compareEmails(email1,email2)
{
	var obj1 = document.getElementById(email1);
	var obj2 = document.getElementById(email2);
	if(obj2.value != "" && obj1.value != obj2.value)
	{
		document.getElementById(email2 + "_msg").innerHTML = "Email does not match";
	}
	else
		document.getElementById(email2 + "_msg").innerHTML = "";
}

function changePassword()
{
	obj = document.getElementById("password_grp");
	if(obj.style.display == "none")
		obj.style.display = "";
	else
		obj.style.display = "none";
}

function checkPassword(fld)
{
	var obj = document.getElementById(fld);
	if(obj.value != "" && obj.value.length < 6 )
	{
		document.getElementById("password_msg").innerHTML = "Minimum 6 characters";
	}
	else
		document.getElementById("password_msg").innerHTML = "";
}

function comparePasswords(fld,fld2)
{
	var obj1 = document.getElementById(fld);
	var obj2 = document.getElementById(fld2);
	if(obj2.value != "" && obj1.value != obj2.value)
	{
		document.getElementById("cpassword_msg").innerHTML = "Password does not match";
	}
	else
		document.getElementById("cpassword_msg").innerHTML = "";
}

function setTerm(term,price)
{
	terms = new Array();
	terms["1"] = "onemonth3";
	terms["3"] = "threemonth3";
	terms["6"] = "sixmonth3";
	terms["12"] = "oneyear3";
	for(var i in terms)
	{
		if(term == i)
			document.getElementById(terms[i]).className = "selected";
		else
			document.getElementById(terms[i]).className = "";
	}
	document.getElementById("c_price").innerHTML = "$" + price;
	document.getElementById("c_term").innerHTML = term < 12 ? term + " month term" : "1 year term";
	setObjValue("term",term);
	setObjValue("price",price);
	setObjValue("c_total","$" + (price * getObjValue("users")));
}

function setUsers(cmd)
{
	if(cmd == "add") 
		new_users = eval(getObjValue("users"))+1;
	else if(cmd == "subtract" && eval(getObjValue("users")) > eval(getObjValue("minimum_users"))) 
		new_users = eval(getObjValue("users"))-1;
		
	setObjValue("users",new_users);
	setObjValue("c_users",new_users);
	total = new_users * getObjValue("price");
	setObjValue("c_total","$" + total);
}

function getDiscount()
{
	thiscode = document.getElementById("discountcode").value;
	if(thiscode != "")
	{
		total = getObjValue("users") * getObjValue("price");
		thisurl = "membership_controller2.php?section=discount&promo=" + thiscode + "&amount=" + total;
		ajax_search(document.form,"pcodemsg",thisurl,false);
	}
	else
		document.getElementById("pcodemsg").innerHTML = "Enter your promo code";	
	return false;
}

function getActivationCode()
{
	if(getObjValue("activation_code") != "")
	{
		thisurl = "membership_controller2.php?section=prepaidaccount&code=" + getObjValue("activation_code");
		ajax_search(document.form,"activation_code_msg",thisurl,false);
	}
	else
		document.getElementById("activation_code_msg").innerHTML = "Enter your activation code";
}

function getMembershipDetails(cmd,val)
{
	p = new Array();
	p.push("section=membershipdetails");
	p.push("fname=" + getObjValue("fname"));
	p.push("email=" + getObjValue("email"));
	p.push("term=" + getObjValue("term"));
	p.push("users=" + getObjValue("users"));
	p.push("discountcode=" + getObjValue("discountcode"));
	p.push("membershiptype=" + getObjValue("membershiptype"));
	p.push("paymenttype=" + getObjValue("payment_type"));
	p.push("currency=" + getObjValue("currency"));
	thisurl = "membership_controller2.php?" + p.join("&");
	//alert(thisurl);
	ajax_search(document.form,"membership_details",thisurl,false,"setCurrencyField()");	
}

function setCurrencyField()
{
	val = getObjValue("payment_type");
	
	if("Visa,MasterCard,Amex,paypal".indexOf(val) == -1)
	{
		//document.getElementById("currency").disabled = true;
		document.getElementById("paynow").style.display = "none";
		document.getElementById("paynow2").style.display = "";
	}
}

function payMembership()
{
	var flds = "card_number,card_sec_code,card_exp_month,card_exp_year,company,address,apartment_info,city,country".split(",");
	//stateprovince
	err = new Array();
	if(in_array(getObjValue("payment_type"), "Visa,MasterCard,Amex".split(",")))
	{
		obj = "";
		if(getObjValue("card_number") == "" || getObjValue("card_number") == "Credit Card Number")
		{
			err.push("Credit Card Number");
			obj = "card_number";
		}
		if(getObjValue("card_sec_code") == "" || getObjValue("card_sec_code") == "Sec. Code")
		{
			err.push("Sec. Code");
			if(obj == "") obj = "card_sec_code";
		}
		if(getObjValue("card_exp_month") == "")
		{
			err.push("Month");
			if(obj == "") obj = "card_exp_month";
		}
		if(getObjValue("card_exp_year") == "")
		{
			err.push("Year");
			if(obj == "") obj = "card_exp_year";
		}
		/*
		if(getObjValue("address") == "" || getObjValue("address") == "Street Address")
		{
			err.push("Street Address");
			if(obj == "") obj = "address";
		}
		if(getObjValue("city") == "" || getObjValue("city") == "Town/City")
		{
			err.push("Town/City");
			if(obj == "") obj = "city";
		}
		
		if(getObjValue("stateprovince") == "")
		{
			err.push("State / Province");
			if(obj == "") obj = "stateprovince";
		}
		
		
		if(getObjValue("country") == "")
		{
			err.push("Country");
			if(obj == "") obj = "country";
		}
		*/
		/*
		if(getObjValue("postalzip") == "" || getObjValue("postalzip") == "Postal Code")
		{
			err.push("Postal Code");
			if(obj == "") obj = "postalzip";
		}
		*/
	}
	
	if(err.length > 0) 
	{
		alert("The following fields are required:\n - " + err.join("\n - "));
		document.getElementById(obj).focus();
		return false;
	}
	else if(in_array(getObjValue("payment_type"), "Visa,MasterCard,Amex,paypal".split(",")) && !document.getElementById("agreement").checked)
	{
		alert("Please click the checkbox to agree to the Terms and Agreement and continue.");
		document.getElementById("agreement").focus();
		return false;
	}
	else
	{
		//document.form.action = "membership_controller2.php";
		//document.form.section.value = "submitpayment";
		
		//disable button
		document.getElementById("paynow").style.display = "none";
		document.getElementById("paynow2").style.display = "none";
		//enable loading button
		document.getElementById("loadingpayment").style.display = "";
			
		document.getElementById("currency").disabled = false;	
		return true;
	}
}

function checkCountry()
{
	getObjValue("country");

}

function checkPaymentType()
{
	var val = getObjValue("payment_type");
	payment_types = "Visa,MasterCard,Amex".split(",");

	/*
	if(val == "cadcheque")
		changeCurrencyOptions(2);
	else if(val == "uscheque")
		changeCurrencyOptions(3);
	else if(val == "westernunion")
		changeCurrencyOptions(4);
	else
		changeCurrencyOptions(1);
	*/
	arr = new Array("cadcheque|cadnote","uscheque|usdnote","westernunion|westernunionnote","paypal|paypalnote");
	for(var i=0;i<arr.length;i++)
	{
		var tmp = arr[i].split("|");
		document.getElementById(tmp[1]).style.display = val == tmp[0] ? "" : "none"
	}
	
	getMembershipDetails('changecurrency');		
	if(in_array(val,payment_types))
	{
		document.getElementById("card_area").style.display = "";
		document.getElementById("signupbilling").style.display = "";
		document.getElementById("noncard_area").style.display = "none";
		document.getElementById("paynow2").style.display = "none";
		document.getElementById("paynow").style.display = "block";
		//curr = (val == "uscheque" || val == "westernunion") ? "USD" : "CAD";
		//setObjValue("currency",curr);
		//getMembershipDetails("changecurrency");
	}
	else
	{
		document.getElementById("card_area").style.display = "none";
		document.getElementById("signupbilling").style.display = "none";
		document.getElementById("noncard_area").style.display = "";
		document.getElementById("currency").disabled = false;
		document.getElementById("paynow2").style.display = "block";
		document.getElementById("paynow").style.display = "none";
	}	
}

function changeCurrencyOptions(val)
{
	option1 = new Array("U.S. Dollars|USD","Euros|EUR","Pounds Sterling|GBP","Japanese Yen|JPY","Canadian Dollars|CAD");	
	option2 = new Array("Canadian Dollars|CAD");	
	option3 = new Array("U.S. Dollars|USD");	
	option4 = new Array("U.S. Dollars|USD","Canadian Dollars|CAD");	
	var fld=document.getElementById("currency");
	fld.options.length = null;
	arr = eval("option" + val);
	for(var i=0;i<arr.length;i++)
	{
		opt = arr[i].split("|");
		sel = i == 0 ? "selected" : "";
		fld.options[i] = new Option(opt[0],opt[1],sel);
	}
}

function updateMyAccount()
{
	valid = true;
	errmsg = "";
	pass = getObjValue("currentpassword");
	pass1 = getObjValue("password");
	pass2 = getObjValue("newpassword");
	pass3 = getObjValue("cpassword");
	if(pass1 != "" || pass2 != "" || pass3 != "")
	{
		if(pass1 == "")
		{
			errmsg = "Enter your current password";
			obj = document.getElementById("password");
		}
		else if(pass2 == "")
		{
			errmsg = "Enter your new password";
			obj = document.getElementById("newpassword");
		}
		else if(pass3 == "")
		{
			errmsg = "Enter password confirmation";
			obj = document.getElementById("cpassword");
		}
		else if(pass2.length < 6)
		{
			errmsg = "New password requires at least 6 characters";
			obj = document.getElementById("newpassword");
		}
		else if(pass2 != pass3)
		{
			errmsg = "New password and password confirmation must be the same";
			obj = document.getElementById("cpassword");
		}
		else if(pass2 == pass1)
		{
			errmsg = "Current Password and New Password must be different";
			obj = document.getElementById("newpassword");
		}
		else if(pass != pass1)
		{
			errmsg = "Incorrect password";
			obj = document.getElementById("password");
		}
			
		if(errmsg != "")
		{
			valid = false;
			alert(errmsg);
			obj.focus();
		}
		else
			document.getElementById("password").value = pass2;
	}
	
	if(valid)
	{
		p = new Array;
		p.push("firstname=" + escape(getObjValue("fname")));
		p.push("lastname=" + escape(getObjValue("lname")));
		p.push("email=" + escape(getObjValue("email")));
		p.push("organization=" + escape(getObjValue("school")));
		p.push("password=" + escape(getObjValue("password")));
		p.push("newpassword=" + escape(getObjValue("newpassword")));
		p.push("cpassword=" + escape(getObjValue("cpassword")));
		p.push("country=" + escape(getObjValue("country")));
		p.push("newsletter=" + escape(getSelValue(document.form,"newsletter")["array_values"].join(",")));
		p.push("typeofstudents=" + escape(getSelValue(document.form,"typeofstudents")["array_values"].join(",")));
		p.push("studentlevel=" + escape(getSelValue(document.form,"studentlevel")["array_values"].join(",")));
		p.push("schools=" + escape(getSelValue(document.form,"schools")["array_values"].join(",")));
		p.push("languages=" + escape(getSelValue(document.form,"languages")["array_values"].join(",")));
		p.push("schools_ext=" + escape(getObjValue("schools_ext")));
		p.push("languages_ext=" + escape(getObjValue("languages_ext")));
	
		
		thisurl = "my_account_controller2.php?section=myaccount&cmd=update&" + p.join("&");
		//alert(thisurl);
		ajax_search(document.form,"account_profile",thisurl,false);	
	}
	//newWindow(thisurl + "&sid=99" ,"test",550,550,'yes');
	return false;
}

function manageSubAccounts(cmd,id)
{
	if(id == undefined) id = "";
	p = new Array;
	fname = getObjValue("sub_firstname");
	lname = getObjValue("sub_lastname");
	email = getObjValue("sub_email");
	msg = "";
	proceed = true;
	if((cmd == "update" || cmd == "add") && (fname == "" || lname == "" || email == ""))
	{
		msg = "All fields are required";
		proceed = false;
	}
	else if(cmd == "delete")
	{
		proceed = confirm("Please confirm if you really want to remove this user from your sub-accounts");
		if(proceed) msg = "Please wait...";
	}
	document.getElementById("subaccounts_msg").innerHTML = msg;
	document.getElementById("subaccounts_msg2").innerHTML = msg;
	if(proceed)
	{
		p.push("section=subaccounts");
		p.push("cmd=" + cmd);
		p.push("id=" + id);
		p.push("firstname=" + escape(fname));
		p.push("lastname=" + escape(lname));
		p.push("email=" + escape(email));
		
		thisurl = "my_account_controller2.php?" + p.join("&");
		//alert(thisurl);
		ajax_search(document.form,"bottomaccounts",thisurl,false);	
	}
	if(cmd == "add" || cmd == "update") return false;
}

function cancelRecurring()
{
	yn = confirm("Confirm if you really want to cancel auto-renewal for your account.");
	if(yn)
	{
		thisurl = "my_account_controller2.php?section=accountdetails&cmd=cancelrecurringbilling";
		ajax_search(document.form,"account_details",thisurl,false,"location.href='account.php'");
	}
	return false;
}

function disabledButton()
{
	return false;
}

function view_invoice(cmd,id)
{
	if(cmd == "transaction")
		newWindow("_my_account/invoice.php?cmd=" + cmd + "&id=" + id,"traninv",1000,800,'yes');
	else
		newWindow("_my_account/invoice.php?cmd=" + cmd + "&id=" + id,"orderinv",1000,800,'yes');
	return false;
}

