String.prototype.trim = function(){
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

function validateUserName(obj)
{
    var userName = obj.value.trim();
    UserAuthService.ValidateUserName(userName,showValidateResult,validateUserNameError); 
}

function validateUserNameError(result)
{
    //Do nothing if any error, ideally, we should log this error to database.
}

function showValidateResult(result)
{
    if(!result)
    {    
        document.getElementById("lblEmailTaken").style.display = "inline";
    }else
    {
        document.getElementById("lblEmailTaken").style.display = "none";
    }
}

//function showNonMHCValidateResult(result)
//{
//    if(!result)
//    {    
//        toolTipOnOut();
//    }
//}

/**
    Validate email address for join MHC
**/
function ValidateEmailAddress(source,arguments)
{   
    var control = document.getElementById(source.controltovalidate);
    var value = GetValidationValueRecursive(control);
    value = value.trim();    
    if(!isEmailValid(value))
    {
        arguments.IsValid = false;
    }else
    {
        arguments.IsValid = true;
    }    
}

function ValidateBasicEmail(source,arguments)
{
    var control = document.getElementById(source.controltovalidate);
    var value = GetValidationValueRecursive(control);
    value = value.trim();    
    if(!isEmailValid(value))
    {
        arguments.IsValid = false;
    }else
    {
        arguments.IsValid = true;
    }    
}


function isEmailValid(email) {
    var regex = /^[a-z0-9!$'*+\-_]+(\.[a-z0-9!$'*+\-_]+)*@([a-z0-9]+(-+[a-z0-9]+)*\.)+([a-z]{2}|aero|arpa|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|msp|name|net|org|pro|travel)$/;
    return regex.test(email.toLowerCase());
}

//excluding gmail,yahoo,hotmail,aol emails
function isValidBusinessEmail(email)
{
    var regNonBusiness = /^[a-z0-9!$'*+\-_]+(\.[a-z0-9!$'*+\-_]+)*@(gmail|yahoo|hotmail|aol)\.([a-z]{2}|aero|arpa|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|msp|name|net|org|pro|travel)$/;
    return !regNonBusiness.test(email.toLowerCase());
}

function ValidatePassword(source,arguments)
{
    var control = document.getElementById(source.controltovalidate);
    var value = GetValidationValueRecursive(control);
    
    if(!checkPassword(value))
    {
        arguments.IsValid = false;
    }else
    {
        arguments.IsValid = true;
    }    
}

function checkPassword(str)
{
	var myRegxpw = /^[a-zA-Z0-9\(]+$/;	
	if ((str.length <= 5 || str.length >= 21))
	{
		return false;
	}else if (!(myRegxpw.test(str))){
		return false;
	}
	return true;
}

/**
    Get the value recursive of the specified control
**/
function GetValidationValueRecursive(control)
{
    if (typeof(control.value) == "string" && (control.type != "radio" || control.checked == true)) {
        return control.value;
    }
    var i, val;
    for (i = 0; i<control.childNodes.length; i++) {
        val = GetValidationValueRecursive(control.childNodes[i]);
        if (val != "") return val;
    }
    return "";
}

/*
function regInit(){
	checkCountry();
	checkPosition();	
}
*/

function checkCountry() {
    document.getElementById("sts").options[0].selected = true;
	if (document.getElementById("cry").options[1].selected){
		loadSelStates(document.getElementById("sts"));
		document.getElementById("sts").disabled = false;
	}else if (document.getElementById("cry").options[2].selected){
		loadSelProvinces(document.getElementById("sts"));
		document.getElementById("sts").disabled = false;
	}else{
		loadSelStatesProvinces(document.getElementById("sts"));
		document.getElementById("sts").disabled = true;
	}
}


function moveAll(from,to) {
	selectAll(from);
	moveSelected(from,to);	
}	
function selectAll(obj) {
	for (var i=0; i<obj.options.length; i++) {
		obj.options[i].selected = true;
	}
}
function moveSelected(from,to) {
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected) {
			to.options[to.options.length] = new Option( o.text, o.value, false, false);
		}
	}
	for (var i=(from.options.length-1); i>=0; i--) {
		var o = from.options[i];
		if (o.selected) {
			from.options[i] = null;
		}
	}	
	sortList(from);
	sortList(to);	
	chkd();
	buildReceive();
}
function sortList(obj) {
	var o = new Array();
	if (obj.options==null) { return; }
	for (var i=0; i<obj.options.length; i++) {
		o[i] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected) ;
	}
	if (o.length==0) { return; }
	o = o.sort( 
		function(a,b) { 
			if ((a.text+"") < (b.text+"")) { return -1; }
			if ((a.text+"") > (b.text+"")) { return 1; }
			return 0;
			} 
		);
	for (var i=0; i<o.length; i++) {
		obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected);
	}
}
var news = "";
var chkdVal = "";
var input	= document.getElementsByTagName('input');
function chkd(){
	chkdVal = "";
	for(j=0;j<input.length;j++){
		formObj=input[j];
		if(formObj.type.toLowerCase() == "checkbox"){
			if ((formObj.checked)&&(formObj.value!="on")) {
				chkdVal += formObj.value + ",";			
			}
		}
	}
	if (chkdVal != ""){
		chkdVal = chkdVal.substring(0,chkdVal.length-1);
	}
}
function chk(){
	if (chkdVal != ""){
		arChkd = chkdVal.split(",");
		for(j=0;j<input.length;j++){
			formObj=input[j];
			if(formObj.type.toLowerCase() == "checkbox"){
				for (var p=0; p < arChkd.length; p++){
					if (formObj.value ==  arChkd[p])
						formObj.checked = true;
				}
			}
		}
	}
}
function createNewsAopCheckBoxes(str){
    if (str == null || str == "") {
        return;
    }
	var dl			= ",";
	var arIds 		= "";
	if (str != "")
		arIds 		= str.split(dl);
	for(var i = 0; i < arIds.length; i++){
	    var newsAopID = trimString(arIds[i]);
		for(var a = 0; a < aops.length; a++){
			if(newsAopID == aops[a][0]){
				news += aops[a][0] + "," + aops[a][1] + "|";
				if (chkdVal != ""){
					chkdVal += "," + aops[a][0];
				}else{
					chkdVal += aops[a][0];
				}
				break;
			}
		}	
	}
	if (news != "")
		news = news.substring(0,news.length-1);
	buildReceive();	
}
function buildReceive(){
	var arlist 		= document.getElementById("list2");	
	var dl			= "|";
	var dl2			= ",";
	var arNews 		= "";
	if (news != "")
		arNews 		= news.split(dl);
	var content 	= "";
	if((arlist.length + arNews.length)>1){
		content = "<div class=\"p-t-5\"><input type=\"checkbox\" id=\"all\" onclick=\"doAll(this)\"><span class=\"b\">Select/Deselect All</span></div>";
	}
	for (var i=0; i<arlist.length; i++){
		content += "<div class=\"p-t-5\"><input type=\"checkbox\" id=\"em" + i + "\" value=\"" + arlist[i].value + "\">";
		content += arlist[i].text;
		content += "</div>";
	}	
	var val = arlist.length;
	for (var i=0; i<arNews.length; i++){
		var bool = "true";
		var newsChoice = arNews[i].split(dl2);
		for (var x=0; x<arlist.length; x++){//dedup results
			if (newsChoice[0]==arlist[x].value){
				bool = "false";
			}
		}
		if (bool == "true"){
			content += "<div class=\"p-t-5\"><input type=\"checkbox\" id=\"em" + val + "\" value=\"" + newsChoice[0] + "\">";
			content += newsChoice[1];
			content += "</div>";
			val++;
		}
	}
	var el = document.getElementById("choices");
	if(el != null)
	{
	    if(document.all){
	 	    el.innerHTML = content;
	    } else if (document.getElementById){
		    rng = document.createRange();
		    //el = document.getElementById("update");
		    rng.setStartBefore(el);
		    htmlFrag = rng.createContextualFragment(content);
		    while (el.hasChildNodes())
			    el.removeChild(el.lastChild);
		    el.appendChild(htmlFrag);
	    }
	}
	chk();
}
function doAll(obj){
	var bool = obj.checked;
	var i=0;
	while(document.getElementById("em" + i)!=null){
		document.getElementById("em" + i).checked = bool;
		i++;										
	}
}
function getAops(){
	var val = "";
	for (var i=0; i<document.getElementById("list2").length; i++){
		val += document.getElementById("list2")[i].value + ",";
	}
	if (val != ""){
		val = val.substring(0,val.length-1);
	}
	return val;
}
function validateReg(){
	var state			= document.getElementById("sts").value;
	var country			= document.getElementById("cry").value;
	var arArea			= getAops();
	//retrieve Newsletter choices
	chkd();	
	
	 document.getElementById("hidCountry").value = country;
	 document.getElementById("hidState").value = state;
	 document.getElementById("hidAop").value = arArea;
	 document.getElementById("hidAopNews").value = chkdVal;
	 return true;
}

function validateCountry(source,arguments)
{
    arguments.IsValid = true;
    
    var country = document.getElementById("cry").value;
    
    if (country == "")
	{
		arguments.IsValid = false;
	}
}

function validateState(source,arguments)
{
    arguments.IsValid = true;
    
    var country			= document.getElementById("cry").value;
    var state			= document.getElementById("sts").value;
    if((country==1||country==2) && state == "")
    {
		arguments.IsValid = false;
	}
}

function validateAccept(source,arguments)
{
    arguments.IsValid = true;
    var accept = document.getElementById("chkAccept").checked;

	if(!(accept)){
	    arguments.IsValid = false;
	}else
	{
	    arguments.IsValid = true;
	}
}

//Used in Confirm Identity Page
function validateAcceptDisclaimer(source,arguments)
{
    arguments.IsValid = true;
    var chkAcceptDisclaimer = document.getElementById("<%= chkAcceptDisclaimer.ClientID %>");
    if(chkAcceptDisclaimer != null && chkAcceptDisclaimer != "undefined")
    {
        var accept = chkAcceptDisclaimer.checked;

	    if(!(accept)){
	        arguments.IsValid = false;
	    }else
	    {
	        arguments.IsValid = true;
	    }
	}else
	{
	    arguments.IsValid = true;
	}
}


//End autosuggest org list on registration page

function initRegForm() 
{
    var form = document.getElementById("aspnetForm");
    // after successful EditProfile, a successs message will be displayed and 
    // the registration form will not be displayed. Therefore we need to check
    // whether the registration form is dispalyed or not.
   if(document.getElementById("hidCountry") != null)
   {
        selectDropDown(document.getElementById("cry"), document.getElementById("hidCountry").value);
        checkCountry(); // update the state/province dropdown according to the selected country
        //checkOrgCountry();
        selectDropDown(document.getElementById("sts"), form.ctl00_plcContent_hidState.value);
        var obj = document.getElementById("list1");
        selectDropDownMultiple(obj, form.ctl00_plcContent_hidAop.value);
        moveSelected(obj, document.getElementById("list2"));
        createNewsAopCheckBoxes(form.ctl00_plcContent_hidAopNews.value);
    }
}

//function checkOrgCountry()
//{
//    document.getElementById("selOrgState").options[0].selected = true;
//	if (document.getElementById("selOrgCountry").options[1].selected){
//		document.getElementById("st").style.display = "block";
//		document.getElementById("prov").style.display = "none";
//		document.getElementById("nosp").style.display = "none";
//		loadSelStates(document.getElementById("sts"));
//		document.getElementById("sts").disabled = false;
//	}else if (document.getElementById("cry").options[2].selected){
//		document.getElementById("prov").style.display = "block";
//		document.getElementById("st").style.display = "none";
//		document.getElementById("nosp").style.display = "none";
//		loadSelProvinces(document.getElementById("sts"));
//		document.getElementById("sts").disabled = false;
//	}else{
//		document.getElementById("st").style.display = "none";
//		document.getElementById("prov").style.display = "none";
//		document.getElementById("nosp").style.display = "block";
//		loadSelStatesProvinces(document.getElementById("sts"));
//		document.getElementById("sts").disabled = true;
//	}
//}

function ValidateAOP(source,arguments)
{
    if(getAops().trim() == "")
    {
        arguments.IsValid = false;
    }else
    {
        arguments.IsValid = true;
    }    
}

function ShowSelectEmailDialog(wdth)
{
    var pos;
	if (window.innerHeight)
	{
		pos = window.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		pos = document.documentElement.scrollTop;
	}
	else if (document.body)
	{
		pos = document.body.scrollTop;
	}
	newsletter = document.getElementById('divChooseEmail');
	newsletter.style.width = wdth + "px";
	newsletter.style.display = 'block';
	setMaskSize();
	document.getElementById('cover').style.display = 'block';
	newsletter.style.top = (((f_clientHeight() - newsletter.offsetHeight) / 2)+pos) + 'px';
	newsletter.style.left = (f_clientWidth() - newsletter.offsetWidth) / 2 + 'px';
	newsletter.style.zIndex = "1001";
	if(document.frames){
		document.getElementById("fr").style.top = newsletter.style.top;
		document.getElementById("fr").style.left = newsletter.style.left;
		document.getElementById("fr").style.width = newsletter.style.width;
		document.getElementById("fr").style.height = "100px";
		document.getElementById("fr").style.zIndex = "1000";
		document.getElementById("fr").style.display = "block";	
		document.getElementById("fr").style.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
		hideDD("hidden");
	}
}

function closeSelectEmailDialog()
{
	document.getElementById('divChooseEmail').style.display = 'none';
	document.getElementById('cover').style.display = 'none';
	if(document.frames){
		document.getElementById("fr").style.display = "none";
		hideDD("visible");
	}
}


