/*///////////////////// MAINLOGINWELCOME Functions ///////////////////////////////////////////////*/

function checkAccess(customercd,orgcd){
	if(!isAssistant(customercd)){
		if(!isPrimRep(customercd, false)){
			if(!isELG(customercd)){
				if(orgcd != ''){
					//debug('in isORGCD');
					//display Institution Access
					$('inst_section').show();
					//display correct Primary Representative Directory link (w/out contact info)
					$('CEO_URL').href='../memberdll.dll/nextForm?wrp=ceo_search.htm&contact=N';
					$('CEO_section').show();
				}
			}
		}
	}
}


//check if they're an assistant 
function isAssistant(customercd){
	var isAss = false;

	var assURL='../memberdll.dll/customlist?SQLNAME=WHOKNOWSWHO&membercd='+customercd+'&membertype=I&RELATIONSHIP=AssistantFor&wmt=none&whp=ass_header.htm&wbp=ass_list.htm&wnr=ajax_noRec.htm';
	//debug(assURL);
	new Ajax.Request(assURL, {method:'get',asynchronous:false,
		onSuccess:function(content){
			var response = content.responseText || 'NO RECORDS';
			//debug(response);
			if( !response.match('NO RECORDS') ){
				isAssistant = true;
				//debug('found relationships!');
				var assCDs = $A(response.split(','));
				//debug('assCDs: '+assCDs[0]+','+assCDs.length);
				//debug(primRep_inst_url);
				assCDs.each(function(element){
					if(element != ''){
						//debug('element:'+element+'--');
						if(isAss == false){
							isAss = isPrimRep(element, true);	
						}
					}
				});
				
			}
		
		},
		onFailure: function(){
			alert('An AJAX error has occured. Please ensure that you have Javascript-enabled browser and re-load the page to try again.');
		}
	});
	return isAss;
}


//check if they're a primary rep - only gets first org since its assumed they only are prim rep of one org
function isPrimRep(customercd, assistant){
	var isPrim = false;
	
	//give assistant access to edit records
	var giveAccess = false;
	var primRepUrl='../memberdll.dll/customlist?SQLNAME=WHOKNOWSWHO&membercd='+customercd+'&kmembertype=O&RELATIONSHIP=PrimaryContact&wmt=none&whp=primRep_header.htm&wbp=primRep_list.htm&wnr=ajax_noRec.htm&range=1/1';
	new Ajax.Request(primRepUrl, {method:'get',asynchronous:false,
		onSuccess:function(content){
			var response = content.responseText || 'NO RECORDS';
			//debug(response);
			if( !response.match('NO RECORDS') ){
				//if assistant, check if assistant should have access
				if(assistant){
					//debug('doing assistant test!');
					var accessURL = '../memberdll.dll/info?customercd='+customercd+'&wrp=assistantAccess.htm&wmt=none&wnr=ajax_norec.htm';
					//debug('accessURL: '+accessURL);
					new Ajax.Request(accessURL, {method:'get', asynchronous:false,
						onSuccess:function(newContent){
							var newResponse = newContent.responseText || '';
							//debug(newResponse);
							if(newResponse.match('Y')){
								giveAccess = true;
							}else{
								giveAccess = false;
							}
						},
						onFailure: function(){
							alert('An AJAX error has occured. Please ensure that you have Javascript-enabled browser and re-load the page to try again.');
						}
					});				
				}else{
					giveAccess = true;
				}
				
				if(giveAccess){
					var primRep_inst_url = '../memberdll.dll/customlist?SQLNAME=INSTITUTION&ADDWHERE=c.orgcd='+response+' and (w.relationship=\'primaryContact\' or k.typecd in(\'elg\',\'elg_sub\'))&whp=edit_header.htm&wbp=edit_list.htm&sort=c.lastname&range=1/10';
					
					//display correct label 
					$("primRep_inst_url").href = primRep_inst_url;
					if(assistant){
						$('primRep_label').innerHTML = 'Assistant Access';
					}else{
						$('primRep_label').innerHTML = 'Primary Rep Access';
					}
					
					$("primRep_section").show();	
					isPrim = true;
					
					//display correct Primary Representative Directory link (w/ contact info)
					$('CEO_URL').href='../memberdll.dll/nextForm?wrp=ceo_search.htm&contact=Y';
					$('CEO_section').show();
				}
			}
			//debug('primRepOrgs: '+primRepOrgs[0]+','+primRepOrgs.length);
		},
		onFailure: function(){
			alert('An AJAX error has occured. Please ensure that you have Javascript-enabled browser and re-load the page to try again.');
		}
	});
	//debug('prim rep access?'+isPrim);
	return isPrim;
}	
		


//check if they're in ELG
function isELG(customercd){
	//debug('in isELG');
	var isELG = false;
	var ELGURL='../committeedll.dll/indlist?c.typecd=|IN|!elg!|!elg_sub!&wmt=none&whp=isELG_header.htm&wbp=isELG_list.htm&wnr=ajax_norec.htm&sort=name&ic.customercd='+customercd;
	new Ajax.Request(ELGURL, {method:'get',asynchronous:false,
		onSuccess:function(content){
			var response = content.responseText || 'NO RECORDS';
			//debug(response);
			if( !response.match('NO RECORDS') ){
				//debug('here');
				var elgURL = '../committeedll.dll/indfulllist?ic.committeecd=|IN'+response+'&ic.ACTIVE=TRUE&whp=indcommittee.htm&wbp=indcommitteelist.htm&sort=c.lastname&range=1/10';
				//debug(elgURL);
				$('ELG_ELG_url').href = elgURL;
				$('ELG_section').show();
				isELG = true;
				
				//display correct Primary Representative Directory link (w/out contact info)
				$('CEO_URL').href='../memberdll.dll/nextForm?wrp=ceo_search.htm&contact=N';
				$('CEO_section').show();
			}
		},
		onFailure: function(){
			alert('An AJAX error has occured. Please ensure that you have Javascript-enabled browser and re-load the page to try again.');
		}
	});	
	return isELG;
}



//check credit fields and add to BILLTOADDRESS
function checkBillAddress(formName){
	var tempAddr = ''
	if(formName.CREDITFIRSTNAME.value == ''){
		alert("Please enter your billing address first name.");
		return false;
	}else{
		tempAddr += formName.CREDITFIRSTNAME.value;
	}
	
	if(formName.CREDITLASTNAME.value == ''){
		alert("Please enter your billing address last name.");
		return false;
	}else{
		tempAddr += ' '+formName.CREDITLASTNAME.value;
	}
	
	if(formName.CREDITADDRESS1.value == ''){
		alert("Please enter your billing mailing address.");
		return false;
	}else{
		//tempAddr += '\n'+formName.CREDITADDRESS1.value;
		tempAddr += ' '+formName.CREDITADDRESS1.value;
	}
	
	if(formName.CREDITADDRESS2.value != ''){
		tempAddr += '\n'+formName.CREDITADDRESS2.value;
	}
	
	if(formName.CREDITCITY.value == ''){
		alert("Please enter your billing address city.");
		return false;
	}else{
		//tempAddr += '\n'+formName.CREDITCITY.value;
		tempAddr += ' '+formName.CREDITCITY.value;
	}
	
	if ((formName.CREDITCOUNTRY.options[formName.CREDITCOUNTRY.selectedIndex].value == 'USA') || (formName.CREDITCOUNTRY.options[formName.CREDITCOUNTRY.selectedIndex].value == 'US') || (formName.CREDITCOUNTRY.options[formName.CREDITCOUNTRY.selectedIndex].value == ''))
	{
		if(formName.CREDITSTATECD.selectedIndex == 0){
			alert("Please select your billing address state.");
			return false;
		}else{
			tempAddr += ', '+formName.CREDITSTATECD[formName.CREDITSTATECD.selectedIndex].value;
		}
	}
	
	if(formName.CREDITZIP.value == ''){
		alert("Please enter your billing address zip code.");
		return false;
	}else{
		tempAddr += ', '+formName.CREDITZIP.value;
	}
	
	if(formName.CREDITCOUNTRY.value != ''){
		tempAddr += '\n'+formName.CREDITCOUNTRY.value;
	}
	
	if (formName.BILLTOADDRESS)
	{
		formName.BILLTOADDRESS.value = tempAddr;
	}
	//alert(formName.BILLTOADDRESS.value);
	return true;
}

function checkBillingAddress(formName){
	var tempAddr = ''
	var lng = 0;
	if((formName.CREDITFIRSTNAME.value == '') || (formName.CREDITLASTNAME.value == '')){
		alert("Please enter your billing address name.");
		return false;
	}else{
		//formName.BILLINGCONTACTNAME.value = formName.CREDITFIRSTNAME.value+' '+formName.CREDITLASTNAME.value;
		//lng += formName.BILLINGCONTACTNAME.value.length;
	}
	
/*
	if(formName.CREDITADDRESS1.value == ''){
		alert("Please enter your billing mailing address.");
		return false;
	}else{		
		//formName.BILLINGADDRESS1.value = formName.CREDITADDRESS1.value;
		//lng += formName.CREDITADDRESS1.value.length;
	}
	
	if(formName.CREDITADDRESS2.value != ''){
		//formName.BILLINGADDRESS2.value= formName.CREDITADDRESS2.value;
		//lng += formName.CREDITADDRESS2.value.length;
	}
	
	if(formName.CREDITCITY.value == ''){
		alert("Please enter your billing address city.");
		return false;
	}else{
		//formName.BILLINGCITY.value= formName.CREDITCITY.value;
		//lng += formName.BILLINGCITY.value.length;
	}
	
	if(formName.CREDITSTATECD.selectedIndex == 0){
		alert("Please select your billing address state.");
		return false;
	}else{
		//formName.BILLINGSTATECD.value = formName.CREDITSTATECD[formName.CREDITSTATECD.selectedIndex].value;
		//lng += formName.BILLINGSTATECD.value.length;
	}
	
	if(formName.CREDITZIP.value == ''){
		alert("Please enter your billing address zip code.");
		return false;
	}else{
		//formName.BILLINGZIP.value= formName.CREDITZIP.value;
		//lng += formName.BILLINGZIP.value.length;
	}
	if (lng==63)
	{
		//formName.BILLINGCONTACTNAME.value+=' ';
	}
	*/
	return true;
}
