var g_bIsDirty=false;

function onChangeElement(){
	g_bIsDirty=true;
	window.status="";
}

function FRMValidate(frm) {
	var sIt='';
	var validate='';
	if(typeof(frm.AddrReq)=='object' && frm.AddrReq.value!=''){
		validate=AddrCheck('Mail');
	}
	if (validate!=''){AddrFocus(frm,validate); alert('Please, enter:' + validate); sIt='stop';}
	if (typeof(frm.PayMethod)!='undefined'){ if (onlinePayType(frm)!='') sIt='stop';}
	return sIt;
}

function FRMEncoding(){
	if (typeof(document.frmListing.Img1)=="object" && document.frmListing.Img1.value!='' || 
	typeof(document.frmListing.Img2)=="object" && document.frmListing.Img2.value!='') document.frmListing.encoding="multipart/form-data";

}

function UploadFiles(lDocID) // New/Update Doc Client page
{	
	var frm=document.frmListing;
	frm.action ="Doc_upload.asp?uri=1005&action=5&di=" + lDocID + "&site=public";
	frm.submit();
}
			
function RetrieveListingValue(txt,frm) {
	var sInf="";
	if(typeof(txt)=="object")	sInf=txt.value;
	onChangeElement();
	
	if(typeof(frm.Contact)=="object")
		frm.Contact.value=GetValueByKey(sInf,'###name=');
	
	if(typeof(frm.CompanyName)=="object")
		frm.CompanyName.value=GetValueByKey(sInf,'###company=');
	
	if(typeof(frm.MailAddress1)=="object")
		frm.MailAddress1.value=GetValueByKey(sInf,'###addr1=');
	
	if(typeof(frm.MailAddress2)=="object")
		frm.MailAddress2.value=GetValueByKey(sInf,'###addr2=');
	
	if(typeof(frm.MailCity)=="object")
		frm.MailCity.value=GetValueByKey(sInf,'###city=');
	
	if(typeof(frm.MailZip)=="object")
		frm.MailZip.value=GetValueByKey(sInf,'###zip=');
	
	if(typeof(frm.Phone)=="object")
		frm.Phone.value=GetValueByKey(sInf,'###phone=');
	
	if(typeof(frm.PhoneExt)=="object")
		frm.PhoneExt.value=GetValueByKey(sInf,'###ext=');
	
	if(typeof(frm.Fax)=="object")
		frm.Fax.value=GetValueByKey(sInf,'###fax=');
	
	if(typeof(frm.Email)=="object")
		frm.Email.value=GetValueByKey(sInf,'###email=');
		
	if(typeof(frm.MailCountry)=="object" && typeof(frm.MailState)=="object"){
		SetItemInCbo(frm.MailCountry,GetValueByKey(sInf,'###country='),0);
		fillStates(frm,'MailState',eval("frm.MailCountry.value"));
		SetItemInCbo(frm.elements('MailState'),GetValueByKey(sInf,'###state='),0)
		if(frm.MailCountry.value>3) frm.MailStateName.value=GetValueByKey(sInf,'###statename=');
	}
	
 }
// --------------------------------------- functions in 2062.xslt

function CheckAdd(Req,Selected) {
	switch (Req.type) {
		case "text":
			if (isNumFunction(Req.value)){
				if (Req.value==0){} else {
					if (Selected.value==0 || Selected=="" || ! isNumFunction (Selected.value)) Selected.value=Req.value;
					Selected.checked=true;
				}
			}
			break;
		case "checkbox":
			
			if (Req.checked) {
		
				if (Selected.value==0 || Selected=="" || ! isNumFunction (Selected.value)) Selected.value=1;
				Selected.checked=true;
			}
		
			break;
	}
}

function UnCheck(item) {
	var sel, result=false;
	if (typeof(eval("frmManage" + ".o" + item + "_Selected"))!="undefined") {
		sel=eval("frmManage" + ".o" + item + "_Selected");
		switch (sel.type) {
			case "text":
				if (sel.value==0 || ! isNumFunction (sel.value)) result=true;
				break;
			case "checkbox":
				if (! sel.checked) result=true;
				break;
		}
		if (result) {
			Uncheck("o" + item + "_Req");
			Uncheck("o" + item + "_Search");
			Uncheck("o" + item + "_Match");
			Uncheck("o" + item + "_ViewDsp");
		}
		else{
			Check("o" + item + "_ViewDsp", sel.value);
		}
	}
}


function Uncheck(itName ){
	var search;
	try{
			search=eval("frmManage" + "." + itName);
			search.value="";
			search.checked=false;
			}
		catch(e){}
}

function Check(itName, itValue){
	var search;
	try{
			search=eval("frmManage" + "." + itName);
			if (search.value=='')	search.value=itValue; //default = Select value
			search.checked=true;
			}
		catch(e){}
}


function FRMValidateAdm(frm){
	var i, lbl, sel, numsel=0, checklbl=false, match, nummatch=0;
	
	for (i=1; i<100; i++) {
		if (typeof(eval(frm.name + ".o" + i + "_Selected"))=="undefined") {
			i=101;
		} else 
			if (typeof(eval(frm.name + ".o" + i + "_Label"))!="undefined"){
			lbl = eval(frm.name + ".o" + i + "_Label").value;
//select
			if (i<=20) {
				sel = eval(frm.name + ".o" + i + "_Selected").value;
				if (isNumFunction(sel)) {
					if (sel>0) checklbl=true;
				}
			} else {
				if (eval(frm.name + ".o" + i + "_Selected").type=="checkbox") checklbl=eval(frm.name + ".o" + i + "_Selected").checked;
			}
			if (checklbl) {
				checklbl=false;
				numsel++;
				if (lbl == '' || lbl == ' ') {
					alert("Please, enter Labels for rows with selected add column!");
					return "stop";
				}
			}
//match
			if (i<=20) {
				if (typeof(eval(frm.name + ".o" + i + "_Match"))!="undefined") match = eval(frm.name + ".o" + i + "_Match").value;
				if (isNumFunction(match)) {
					if (match>0) checklbl=true;
				}
			} else if (typeof(eval(frm.name + ".o" + i + "_Match"))!="undefined"){
				if (eval(frm.name + ".o" + i + "_Match").type=="checkbox") checklbl=eval(frm.name + ".o" + i + "_Match").checked;
			}
			if (checklbl) {
				checklbl=false;
				nummatch++;
				if (lbl == '' || lbl == ' ') {
					alert("Please, enter Labels for rows with selected match column!");
					return "stop";
				}
			}
		}
	}
	
	if (numsel==0) {
		alert("Please, select at least one field in column Add!");
		return "stop";
	} else {
		if (nummatch==0) {
			alert("Please, select at least one field in column Match!");
			return "stop";
		}
	}
}

function reload(val)
{
	if (val.value=='yes') {
		showHideLayers('ExchangeApplication','show');
	}
	else {
	showHideLayers('ExchangeApplication','hide');
	}	
}

function showHideLayers(ID,v){
if(document.getElementById(ID)!=null) document.getElementById(ID).style.display=(v=='show')?'':'none';
}

function RequiredFlds1(){ // It is the same as into submit.js , but different message
	var sRtn="";

	for(i=1; i<100; i++){
		try{
			ctrl=eval("document.getElementById('Verify" + i + "')")
			if(ctrl.value==''){
				var ctrName=ctrl.className!=''?ctrl.className:ctrl.name;
				alert("Please complete all required field(s) marked by the asterisk")
				ctrl.focus();
				sRtn="stop";
				break;
			}
		}
		catch(e){}
	}
	return sRtn;
}

function SearchValidation()
{	
	var f = document.getElementById('frmExListSearch'); // get the form as ID And then lookup for the forms Items
	var val =false;

	var ReqSrchCriteria=document.frmExListSearch.ReqSrchCriteria;

	 if (ReqSrchCriteria.value!=0)
		{		
		for( var i = 0; i < f.elements.length; i++ )  
				{				
					var elname=f.elements[i].name;
				
					if (elname!='items' &&  elname.substr(0,3).toUpperCase()!="REQ"){
						if (f.elements[i].type=="text" || f.elements[i].type=="select-one" || f.elements[i].type=="select-multiple")
						{			
						var oFld=eval("document.frmExListSearch." + f.elements[i].name);
						
						if ( oFld.value!='') {
							//alert(f.elements[i].name + '//' + f.elements[i].type + '//' + oFld.value);
							val=true;
							}
						}
					}					
				 } 
		}
	else
		{val=true;}
				
	return val;				
}
function SearchValidationReqFields()
{	
	
	var f = document.getElementById('frmExListSearch'); // get the form as ID And then lookup for the forms Items
	var val =true;
	var elname;

	for( var i = 0; i < f.elements.length; i++ )  
	{				
		elname=f.elements[i].name;
			
	// eliminate required controls
		if (elname!='items' && elname.substr(0,3)!="req")
			{
				if (f.elements[i].type=="text" || f.elements[i].type=="select-one" || f.elements[i].type=="select-multiple")
				{	
					
				var oFld=eval("document.frmExListSearch." + f.elements[i].name);
				var oFldReq=eval("document.frmExListSearch.req" + f.elements[i].name);
				
				if 	(typeof(oFldReq)=="object")
						{
						if (oFldReq.value=='True' && oFld.value=='')
								return false;
						}
				}
			}		
	 } 
	 return val;
}
function UseApplet(val)
{
	
	var chk=document.getElementById('UseApplett');
	if (val.value==0)
	{
		chk.value=-1;
	}
	else
	{
		chk.value=0;
	}
	
}

function SubmitListing(val, allowtypes, chkEnable, frmAction) {
	var captchaValue = "";
	var captcha_wrapper = document.getElementById("CaptchaWrapper");
	var TrmsAccepted;
	var terms_wrapper;

	if (RequiredFldsListing()!="") return;

	// Check if the "Captcha" control exists	
	if (typeof(document.frmListing.captchacode)=="object") {
		captchaValue = document.getElementById("captchacode").value;
		captchaValue = captchaValue.replace(/ /g,"").toUpperCase();
		
		if (captchaValue == "") {
			// Display "no value" message
			alert("Please enter the code shown in the picture.");
			
			// Highlight either the captcha table or captcha input field
			if (captcha_wrapper != null) {
				captcha_wrapper.style.backgroundColor = "#FFFF33";
			}
			else {
				document.getElementById("captchacode").style.backgroundColor = "#FFFF33";
			}
			
			// Set focus to captcha input field
			document.getElementById("captchacode").focus();
		}
		else {
			// Set background colors to standard of either the captcha table or captcha input field
			if (captcha_wrapper != null) {
				captcha_wrapper.style.backgroundColor = "#ECEFF4";
			}
			else {
				document.getElementById("captchacode").style.backgroundColor = "#ECEFF4";
			}

			document.getElementById("captchacode").value = captchaValue;
			
			if (window.XMLHttpRequest) {
					// code for IE7+, Firefox, Chrome, Opera, Safari
			  	xmlhttp=new XMLHttpRequest();
			}
			else {
					// code for IE6, IE5
			  	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			
			xmlhttp.onreadystatechange=function() {
			  if (xmlhttp.readyState==4 && xmlhttp.status==200) {
						if (xmlhttp.responseText == 'True'){
// CAPTCHA CONFIRMED CODE	PATH	
							// Check if the "Terms" control exists	
							if (typeof(document.frmListing.AcceptedTerms)=="object") {	
								// Get the status of the "Terms" control - "checked" or "!checked"
								TrmsAccepted = document.frmListing.AcceptedTerms.checked;
								terms_wrapper = document.getElementById("TermsWrapper");
  						
								if (TrmsAccepted) {
									if (terms_wrapper != null) {
										terms_wrapper.style.backgroundColor = "#ECEFF4";
									}
									if(chkEnable=='False') {
										// Submit listing
										_onSubmitListing(document.frmListing, frmAction);
									}
									else {
										if (typeof(document.frmListing.attachment)=="object") {	
											var file=val.value;
											
											allowtypes = allowtypes.toLowerCase();
											var ext=file.substring(file.lastIndexOf('.') + 1).toLowerCase();
											var ind=allowtypes.indexOf(ext);
											if(val.value!='') {
												if (ind==-1 && allowtypes!=',,') {
													alert('File type not allowed.');
												}
												else {
													// Submit listing
													_onSubmitListing(document.frmListing, frmAction);
												}
											}
											else {
												_onSubmitListing(document.frmListing, frmAction);
											}
										}										
									else {
										_onSubmitListing(document.frmListing, frmAction);
									}
									}
								}
								else {
									if (terms_wrapper != null) {
										terms_wrapper.style.backgroundColor = "#FFFF33";
									}
									document.frmListing.AcceptedTerms.focus();
									alert('You must accept the terms and conditions.');									
								}
							}
							// "Terms" control does not exist
							else {
								if(chkEnable=='False') {
									// Submit listing
									_onSubmitListing(document.frmListing, frmAction);
								}
								else {
									if (typeof(document.frmListing.attachment)=="object") {	
										var file=val.value;
										
										allowtypes = allowtypes.toLowerCase();
										var ext=file.substring(file.lastIndexOf('.') + 1).toLowerCase();
										var ind=allowtypes.indexOf(ext);
										if(val.value!='') {
											if (ind==-1 && allowtypes!=',,') {
												alert('File type not allowed.');
											}
											else {
												// Submit listing
												_onSubmitListing(document.frmListing, frmAction);
											}
										}
										else {
											// Submit listing
											_onSubmitListing(document.frmListing, frmAction);
										}
									}										
								}
							}	
						}
						else {
// WRONG CAPTCHA CODE	PATH
							// Display error message
							alert('Security code not verified.');
							
							// Highlight either the captcha table or captcha input field
							if (captcha_wrapper != null) {
								captcha_wrapper.style.backgroundColor = "#FFFF33";
							}
							else {
								document.getElementById("captchacode").style.backgroundColor = "#FFFF33";
							}
							
							// Set focus to captcha input field
							document.getElementById("captchacode").focus();
						}
			    }
			  }
			xmlhttp.open("GET", "test.asp?uri=1043&action=666&code=" + captchaValue + '&time=' + new Date().getTime(), true);
			xmlhttp.send();
		}
	}
	// "Captcha" control does not exist
	else {
		if (typeof(document.frmListing.AcceptedTerms)=="object") {	
			// Get the status of the "Terms" control - "checked" or "!checked"
			TrmsAccepted = document.frmListing.AcceptedTerms.checked;
  		terms_wrapper = document.getElementById("TermsWrapper");
  		
			if (TrmsAccepted) {
				if (terms_wrapper != null) {
					terms_wrapper.style.backgroundColor = "#ECEFF4";
				}
				
				if(chkEnable=='False') {
					// Submit listing
					_onSubmitListing(document.frmListing, frmAction);
				}
				else {
					if (typeof(document.frmListing.attachment)=="object") {	
						var file=val.value;
						
						allowtypes = allowtypes.toLowerCase();
						var ext=file.substring(file.lastIndexOf('.') + 1).toLowerCase();
						var ind=allowtypes.indexOf(ext);
						if(val.value!='') {
							if (ind==-1 && allowtypes!=',,') {
								alert('File type not allowed.');
							}
							else {
								// Submit listing
								_onSubmitListing(document.frmListing, frmAction);
							}
						}
						else {
							// Submit listing
							_onSubmitListing(document.frmListing, frmAction);
						}
					}										
					else {
						// Submit listing
						_onSubmitListing(document.frmListing, frmAction);
					}
				}
			}
			else {
				if (terms_wrapper != null) {
					terms_wrapper.style.backgroundColor = "#FFFF33";
				}
				document.frmListing.AcceptedTerms.focus();
				alert('You must accept the terms and conditions.');									
			}
		}
		// "Terms" control does not exist
		else {
			if(chkEnable=='False') {
				// Submit listing
				_onSubmitListing(document.frmListing, frmAction);
			}
			else {
				if (typeof(document.frmListing.attachment)=="object") {	
					var file=val.value;
					
					allowtypes = allowtypes.toLowerCase();
					var ext=file.substring(file.lastIndexOf('.') + 1).toLowerCase();
					var ind=allowtypes.indexOf(ext);
					if(val.value!='') {
						if (ind==-1 && allowtypes!=',,') {
							alert('File type not allowed.');
						}
						else {
							// Submit listing
							_onSubmitListing(document.frmListing, frmAction);
						}
					}
					else {
						// Submit listing
						_onSubmitListing(document.frmListing, frmAction);
					}
				}	
				else {
					// Submit listing
					_onSubmitListing(document.frmListing, frmAction);
				}
			}
		}			
	}
}

// the function check if the allow file type add to listing
function checkExt(val, allowtypes, chkEnable)
{
	var bAllow=0;

	if(chkEnable=='False') {return 0; }
	
	if (typeof(document.frmListing.attachment)=="object")
	{	
		var file=val.value;
		
		allowtypes = allowtypes.toLowerCase();
		var ext=file.substring(file.lastIndexOf('.') + 1).toLowerCase();
		var ind=allowtypes.indexOf(ext);
		if(val.value!='')
		{
			if (ind==-1 && allowtypes!=',,') {
			
				alert('Not allowed file type.');
				bAllow=-1;
			}
		}
	}
	return bAllow;
}

function ValidateEmail(val) {
	if (val.value.indexOf ('@',0) == -1 || val.value.indexOf ('.',0) == -1)	{
  	alert("Please enter a valid e-mail address.");
  	val.style.backgroundColor="#FFFF33";
	}
	else {
		val.style.backgroundColor="";
	}
}

// search for 1041  
function _srch(){

	
		if (SearchValidationReqFields()==false)
		{
		alert('Please complete all required field(s) marked by the asterisk'); 
		return;
		}
		if (SearchValidation()==false)
		{
		alert('Please enter a value in at least one of the search criteria fields'); 
		return;
		}
		
	

	var act = document.location.href;
	var pos= act.indexOf('&ei=');
	var next=act.indexOf('&', pos+1);
	var eid;
	if (next==-1){eid=act.substring(pos);}
	else{eid=act.substring(pos, next);}

	var pos2= act.indexOf('&par=');
	var next2=act.indexOf('&', pos2+1);
	var eid2;
	
	var pos3= act.indexOf('&ci=');
	var next3=act.indexOf('&', pos3+1);
	var ci;
	
	if (pos2==-1){
		eid2 = '';
	}
	else {
		if (next2==-1){
			eid2=act.substring(pos2);
			}
		else{
			eid2=act.substring(pos2, next2);
			}
	}
	if (pos3==-1){
		ci = '';
	}
	else {
		if (next3==-1){
			ci=act.substring(pos3);
			}
		else{
			ci=act.substring(pos23, next3);
			}
	}
	
	document.frmExListSearch.action= "main.asp?uri=1041" + eid + eid2 + ci+ '&fi=1';
	document.frmExListSearch.submit();
   
}




function _clear() {
	var act = document.location.href;
	var pos= act.indexOf('&ei=');
	var next=act.indexOf('&', pos+1);
	var eid;
	if (next==-1){eid=act.substring(pos);}
	else{eid=act.substring(pos, next);}
	document.frmExListSearch.action= "main.asp?uri=1041" + eid;
	document.frmExListSearch.submit();

}

function CopyFromProfile(lExchID, lListID, ParListID, profile)
{
	var url="main.asp?uri=1043&ei=" + lExchID;
	if (lListID!=0){url= url + "&li=" + lListID;}
	if(typeof(ParListID)=="undefined") ParListID=0;
	if (ParListID!=0){url= url + "&parlst=" + ParListID;}
	if (profile==true)
	{
		url= url + "&prf=yes";
	}
	var selprf=document.getElementById('ProfileSelection');

	document.frmListing.action = url + "&fromprf=" + selprf.value;
	document.frmListing.submit();
}
function CheckSavePrfOption(chk)
{
	
	if (!chk.checked)	showHideLayers('ShowProfileNameBox','hide');
	else			showHideLayers('ShowProfileNameBox','show');
	
		
}
function reloadSkipLogin(val)
{
	if (val.value=="yes")	showHideLayers('divQuickRegForm','hide');
	else			showHideLayers('divQuickRegForm','show');
	}
