function ABCheckEmail(str) {
	if(!str.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi)) {
		return false;
	}
	return true;
}
//--------------
function ABCheckLength(str,len) {
	if(str.length < len) {
		return false;
	}
	return true;
}
//--------------
function ABCheckPassword(str) {
	if (str.length < 6) {return false;}//too short
	var a = 'abcdefghijklmnopqrstuvxyz';
	var b = 'ABCDEFGHIJKLMNOPQRSTUVXYZ';
	var c = '1234567890';
	var aa = null;
	var bb = null;
	var cc = null;
	for (i in str) {
		if (aa == null) {aa = a.match(str[i]);}
		if (bb == null) {bb = b.match(str[i]);}
		if (cc == null) {cc = c.match(str[i]);}
	}
	if (aa != null && bb != null && cc != null) {
		return true;
	}
	return false;
}
//--------------
function ABValidateForm(form) {
	alert(form.attr('id'));
	var valid = [];
	var ok = '';
	var elems = form.find("[title='required']");
	for (i in elems) {
		ok = false;
		//alert($(elems[i]).attr('id'));
		if ($(elems[i]).attr('alt')=='email') {
			ok = ABCheckEmail($(elems[i]).val());
		}	
		if (typeof($(elems[i]).attr('alt'))=='number') {
			ok = ABCheckLength($(elems[i]).val(),$(elems[i]).attr('alt'));
		}
		if (typeof($(elems[i]).attr('alt'))=='password') {
			ok = ABCheckPassword($(elems[i]).val());
		}
		if (ok==false) {
			$(elems[i]).addClass('error');
			valid[i] = 0;
		} else {
			$(elems[i]).removeClass('error');
			valid[i] = 1;
		}
	}	
	for (j in valid) {
		if (valid[j] == 0) {
			return false;
		}	
	}
	return true;
}
/*
var slid = '2';
var req = jQuery.ajax({type: 'GET', url:"/extensions/referent/banner.php?slid="+slid+"",success: function(data){jQuery("#topslot").html( html );}});req=null;
*/
//--------------
function AWIEditor(append_elem,content) {
	if (!append_elem.offset()) append_elem = jQuery(document);
	var os = append_elem.offset();
	jQuery('#awieditor').remove();
	jQuery('body').append('<div id="awieditor" style="background:#ffffff;height:auto;width:auto;position:absolute;z-index:100;visibility:hidden;font-family: Arial, Helvetica, sans-serif;font-size:11px;font-color:#666666;border-color:#cccccc;border-style:solid;border-bottom-width:1px;border-left-width:1px;border-right-width:1px;border-top-width:1px;vertical-align:top;padding-right:5px;padding-left:5px;padding-bottom:5px;"></div>');
	jQuery('#awieditor').css('visibility', 'hidden');
	//var req = jQuery.ajax({type: 'GET', url:url,success: function(data){jQuery('#awieditor').html(data);}});req=null;
	jQuery('#awieditor').html('<a href="#" style="margin:0px;padding:0px;vertical-align:top;float:right;text-decoration:none;" onClick="jQuery(\'#awieditor\').remove();"><b>&times;</b></a><br>'+append_elem.attr('menus'));
	jQuery('#awieditor').css('left', os.left).css('top', os.top).css('visibility', 'visible');
	return false;
}
//--------------
function AWIEditor2(append_elem,content,padleft,padtop,width,editorid) {
	if (!append_elem.offset()) append_elem = jQuery(document);
	var os = append_elem.offset();
	jQuery('#'+editorid).remove();
	if (typeof(width) == "undefined") {
		width = "95%";	
	}
	if (typeof(editorid) == "undefined") {
		editorid = "awieditor";	
	}
	jQuery('body').append('<div id="'+editorid+'" style="background:#ffffff;height:auto;width:'+width+';position:absolute;z-index:100;visibility:hidden;font-family: Arial, Helvetica, sans-serif;font-size:11px;font-color:#666666;border-color:#cccccc;border-style:solid;border-bottom-width:1px;border-left-width:1px;border-right-width:1px;border-top-width:1px;vertical-align:top;padding-right:5px;padding-left:5px;padding-bottom:5px;"></div>');
	jQuery('#'+editorid).css('visibility', 'hidden');
	//var req = jQuery.ajax({type: 'GET', url:url,success: function(data){jQuery('#awieditor').html(data);}});req=null;
	jQuery('#'+editorid).html('<a id="awieditorclose" href="#" style="margin:0px;padding:0px;vertical-align:top;float:right;text-decoration:none;" onClick="jQuery(\'#'+editorid+'\').remove();"><b>&times;</b></a><br>'+content);
	if (typeof(padleft) != "undefined") {
		padleft = parseInt(padleft);	
	} else {
		padleft = 0;	
	}
	if (typeof(padtop) != "undefined") {
		padtop = parseInt(padtop);	
	} else {
		padtop = 0;	
	}
	jQuery('#'+editorid).css('left', os.left + padleft).css('top', os.top + padtop).css('visibility', 'visible');
	return false;
}
//--------------------
function AWIEditor3(append_elem,content,padleft,padtop,width,editorid) {
	if (!append_elem.offset()) append_elem = jQuery(document);
	var os = append_elem.offset();
	jQuery('#'+editorid).remove();
	if (typeof(width) == "undefined") {
		width = "95%";	
	}
	if (typeof(editorid) == "undefined") {
		editorid = "awieditor";	
	}
	jQuery('body').append('<div id="'+editorid+'" style="background:#ffffff;height:auto;width:'+width+';position:absolute;z-index:100;visibility:hidden;font-family: Arial, Helvetica, sans-serif;font-size:11px;font-color:#666666;border-color:#cccccc;border-style:solid;border-bottom-width:1px;border-left-width:1px;border-right-width:1px;border-top-width:1px;vertical-align:top;padding-right:5px;padding-left:5px;padding-bottom:5px;"></div>');
	jQuery('#'+editorid).css('visibility', 'hidden');
	//var req = jQuery.ajax({type: 'GET', url:url,success: function(data){jQuery('#awieditor').html(data);}});req=null;
	jQuery('#'+editorid).html('<a id="awieditorclose" href="#" style="margin:0px;padding:0px;vertical-align:top;float:right;text-decoration:none;" onClick="jQuery(\'#'+editorid+'\').remove();return false;"><b>&times;</b></a><br><iframe frameborder="0" height="600" width="600" scrolling="no" src="'+content+'"');
	if (typeof(padleft) != "undefined") {
		padleft = parseInt(padleft);	
	} else {
		padleft = 0;	
	}
	if (typeof(padtop) != "undefined") {
		padtop = parseInt(padtop);	
	} else {
		padtop = 0;	
	}
	jQuery('#'+editorid).css('left', os.left + padleft).css('top', os.top + padtop).css('visibility', 'visible');
	return false;
}
//--------------
function AWImg(append_elem,content,hash) {
	var os = append_elem.offset();
	jQuery('#awieditor').remove();
	jQuery('body').append('<div onClick="jQuery(this).hide();" id="awieditor" style="background:#ffffff;height:auto;width:auto;position:absolute;z-index:999;visibility:hidden;font-family: Arial, Helvetica, sans-serif;font-size:11px;font-color:#666666;border-color:#cccccc;border-style:solid;border-bottom-width:1px;border-left-width:1px;border-right-width:1px;border-top-width:1px;vertical-align:top;padding:20px;text-align:center;"></div>');
	jQuery('#awieditor').css('visibility', 'hidden');
	jQuery('#awieditor').html(content);
	var left = screen.width / 4;
	jQuery('#awieditor').css('left', /*os.left*/'300px').css('top', /*os.top*/'120px').css('visibility', 'visible');
	if (hash != '') {
		document.location.hash = hash;
	}
	return false;
}
//--------------
function AWImg2(append_elem,content,hash) {
	var os = append_elem.offset();
	window.parent.jQuery('#awieditor').remove();
	window.parent.jQuery('body').append('<div onClick="jQuery(this).hide();" id="awieditor" style="background:#ffffff;height:auto;width:auto;position:absolute;z-index:999;visibility:hidden;font-family: Arial, Helvetica, sans-serif;font-size:11px;font-color:#666666;border-color:#cccccc;border-style:solid;border-bottom-width:1px;border-left-width:1px;border-right-width:1px;border-top-width:1px;vertical-align:top;padding:20px;text-align:center;"></div>');
	window.parent.jQuery('#awieditor').css('visibility', 'hidden');
	window.parent.jQuery('#awieditor').html(content);
	var left = screen.width / 4;
	window.parent.jQuery('#awieditor').css('left', /*os.left*/'300px').css('top', /*os.top*/'120px').css('visibility', 'visible');
	if (hash != '') {
		window.parent.location.hash = hash;
	}
	return false;
}
//--------------
function popup(url, w, h) {
	var h2 = h + 25;
	var w2 = w + 20
	var winl = 50;
	var wint = 50;
	var mypage = window.open(url, "mypage", "height="+h2+",width="+w2+",top="+wint+",left="+winl+",scrollbars=yes,menubar=yes,resizable=yes");
	return false;
}
//--------------
function trToggle() {
	jQuery('tr[id^="tr_toggle_"]').toggle();	
}
//--------------
function toggleImg(iid) {
	if(jQuery('#ipi_'+iid).attr('src')=='/extensions/referent/px_referent/images/plus.gif'){
		jQuery('#ipi_'+iid).attr('src','/extensions/referent/px_referent/images/minus.gif');
	} else{
		jQuery('#ipi_'+iid).attr('src','/extensions/referent/px_referent/images/plus.gif');
	}
	return false;
}

var cache1 = new Array();//srch opts cache important
var cache2 = new Array();//regular opts cache important
/*
jQuery('select[lang="search"]').each(function(){//init load cache
	var theoption = jQuery(this);
	var thename = jQuery(this).attr('name');
	jQuery(this).children().each(function(){
		var theval = jQuery(this).attr('value');
		jQuery('select[lang="search"][class="awi_'+thename+'"]').each(function() {
			initCache(jQuery(this).attr('name'),thename,theval);
		});
	});	
	return;//break after first loop
});
*/
//--------------
jQuery(document).ready(function() {
	if (jQuery.browser.msie) jQuery('div[id^="rightslot"]').css('position','relative').css('top','155px');
	//if (getURLVar('act') != 'search') {
		jQuery('tr[lang!=""]').hide();
	//}
	//jQuery('tr[parelem!=""]').hide();//init hide in search.tpl
	jQuery.ajaxSetup({ 
        scriptCharset: "utf-8" , 
        contentType: "application/x-www-form-urlencoded; charset UTF-8"
	});
	jQuery('select[lang!="search"]').change(function() {//alert('select:'+jQuery(this).attr('name'));
		//if (jQuery(this).val() == null || typeof(jQuery(this).val()) == 'undefined') return;
		var theoption = jQuery(this);
		var thename = jQuery(this).attr('name');
		var theval = jQuery(this).val();
		jQuery('select[lang!="search"][class="awi_'+thename+'"]').each(function() {
			getOpts(jQuery(this).attr('name'),thename,theval,0,'');
			jQuery(this).change();
		});
		jQuery('#error_'+thename).html('');
	});
	jQuery('select[lang="search"]').change(function() {//alert('select:'+jQuery(this).attr('name'));
		//cascading selects
		jQuery('tr[lang!=""]').each(function(){
			var thetr = jQuery(this);									
			//var theone = jQuery(this).find('select');
			if (jQuery(this).attr('lang').indexOf('|') != -1) {
				var spl = new Array();
				spl = jQuery(this).attr('lang').split('|');
				jQuery('select[lang="search"][name="'+spl[0]+'"]').each(function(){
					thetr.hide();//init hide
					if (spl[1].indexOf('-') != -1) {//multiple parent values
						spl2 = spl[1].split('-');
						for (i in spl2) {
							if (jQuery(this).is(':visible') && jQuery(this).val() == spl2[i]) {
								thetr.show();
								
							} //else {
								//thetr.hide();
							//}														 
						}
					} else {
						if (jQuery(this).is(':visible') && jQuery(this).val() == spl[1]) {
							thetr.show();
							
						}// else {
							//thetr.hide();
						//}														 
					}
				});
			} else {
				jQuery('select[lang="search"][name="'+jQuery(this).attr('lang')+'"]').each(function(){//alert(theone);
					if (jQuery(this).is(':visible') && trim(jQuery(this).html()+'') != '' && trim(jQuery(this).val()+'') != '' && trim(jQuery(this).val()) != '0' && trim(jQuery(this).val()) != null) {
						thetr.show();
					} else {
						thetr.hide();
					}																				   
				});
			}	
		});
		//#cascading selects
		
		var theoption = jQuery(this);
		var thename = jQuery(this).attr('name');
		var theval = jQuery(this).val();
		//if (thename == 'search_category') {
			var search_category = '&search_category='+jQuery('#awiselsrch_search_category').val();
		//} else {
			//var search_category = '';
		//}
		//if (thename != 'search_category') {//search_category - exception
			jQuery('select[lang="search"][class="awi_'+thename+'"]').each(function() {
				getOpts(jQuery(this).attr('name'),thename,theval,1,search_category);
				jQuery(this).change();
			});
		//}
		jQuery('#error_'+thename).html('');
	});
});
		/*
		jQuery(document).ajaxStop(function(){
			jQuery('select:empty[lang="search"]').each(function(){
				jQuery(this).parents('tr[parelem!=""]').hide();
			});	
		});
		*/
//--------------------------------
function getURLVar(urlVarName) {
	//divide the URL in half at the '?'
	var urlHalves = String(document.location).split('?');
	var urlVarValue = '';
	if(urlHalves[1]){
		//load all the name/value pairs into an array
		var urlVars = urlHalves[1].split('&');
		//loop over the list, and find the specified url variable
		for(i=0; i<=(urlVars.length); i++){
			if(urlVars[i]){
				//load the name/value pair into an array
				var urlVarPair = urlVars[i].split('=');
				if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
					//I found a variable that matches, load it's value into the return variable
					urlVarValue = urlVarPair[1];
				}
			}
		}
	}
	return urlVarValue;   
}
//--------------------------------
function initCache(on,pn,pv) {//not in use
	if (typeof(cache[on]) == 'undefined') {
		cache[on] = new Array();
	}
	if (typeof(cache[on][pn]) == 'undefined') {
		cache[on][pn] = new Array();
	}
	if (typeof(cache[on][pn][pv]) == 'undefined') {
		cache[on][pn][pv] = new Array();
	}
	jQuery.ajax({
	   type: "POST",
	   url: '/extensions/referent/get_options.php',
	   data: "on="+on+"&pn="+pn+"&pv="+pv+"&id="+getURLVar('id'),
	   dataType:"html",
	   processData:false,
	   success: function(data){
			var ops = data.split('<b>');
			var pops = '';
			var tmp = '';
			for (i in ops) {
				try {
					tmp = ops[i].split('<a>');
					pops += '<option value="'+tmp[0]+'" class="'+tmp[1]+'" '+tmp[2]+'>'+tmp[3]+'</option>';
				} catch(err) {}
			}
			delete cache[on][pn][pv];
			cache[on][pn][pv] = pops;
	   }
	 });
}
//--------------------------------
function getOpts(on,pn,pv,ln,search_category) {
	//srch cache
	if (typeof(cache1[on]) == 'undefined') {
		cache1[on] = new Array();
	}
	if (typeof(cache1[on][pn]) == 'undefined') {
		cache1[on][pn] = new Array();
	}
	if (typeof(cache1[on][pn][pv]) == 'undefined') {
		cache1[on][pn][pv] = new Array();
	}
	if (typeof(cache1[on][pn][pv][search_category]) == 'undefined') {
		cache1[on][pn][pv][search_category] = '';
	}
	//regular cache
	if (typeof(cache2[on]) == 'undefined') {
		cache2[on] = new Array();
	}
	if (typeof(cache2[on][pn]) == 'undefined') {
		cache2[on][pn] = new Array();
	}
	if (typeof(cache2[on][pn][pv]) == 'undefined') {
		cache2[on][pn][pv] = '';
	}
	jQuery('#indicator').remove();
	if (ln == 1) {
		var the_cache = cache1[on][pn][pv][search_category];
		if (the_cache.length > 0) {
			jQuery('#awiselsrch_'+on).html('').append(the_cache);
			return;
		}
		jQuery('#awiselsrch_'+on).html('').parent().append('<img id="indicator" src="/extensions/referent/px_referent/images/indicator.gif" vspace="0" hspace="0" border="0" align="top">'); 
	} else {
		var the_cache = cache2[on][pn][pv];
		if (the_cache.length > 0) {
			jQuery('#awisel_'+on).html('').append(the_cache);
			return;
		}
		jQuery('#awisel_'+on).html('').parent().append('<img id="indicator" src="/extensions/referent/px_referent/images/indicator.gif" vspace="0" hspace="0" border="0" align="top">'); 
	}
	var loc = window.location;
		//if (pn=='search_category' && getURLVar('argo')=='1') alert("/extensions/referent/get_options.php?on="+on+"&pn="+pn+"&pv="+pv+"&id="+getURLVar('id')+search_category);

	jQuery.ajax({
	   type: "POST",
	   url: '/extensions/referent/get_options.php',
	   data: "on="+on+"&pn="+pn+"&pv="+pv+"&id="+getURLVar('id')+search_category,
	   dataType:"html",
	   processData:false,
	   success: function(data){
			var ops = data.split('<b>');
			var pops = '';
			var tmp = '';
			var mx = 0;
			for (i in ops) {
				//if (tmp[0] != 'undefined' && tmp[1] != 'undefined' && tmp[2] != 'undefined' && tmp[3] != 'undefined')
				try {
					tmp = ops[i].split('<a>');
					//if (i > 0) {
						//sel = '';	
					//}
					pops += '<option value="'+tmp[0]+'" class="'+tmp[1]+'" '+tmp[2]+'>'+tmp[3]+'</option>';
					if (tmp[3].length > mx) {
						mx = trim(tmp[3]).length;	
					}
				} catch(err) {}
			}
			if (mx > 0 && jQuery.browser.msie){
				mx *= 7;
				mx += 20;
				if (ln == 1) {
					jQuery('#awiselsrch_'+on).css('width',mx+'px');//f*ck microsoft
				} else {
					jQuery('#awisel_'+on).css('width',mx+'px');//f*ck microsoft
				}
			}
			jQuery('#indicator').remove();
			if (ln == 1) {
				jQuery('#awiselsrch_'+on).html('').append(pops); 
			} else {
				jQuery('#awisel_'+on).html('').append(pops); 
			}
			if (ln == 1) {
				delete cache1[on][pn][pv][search_category];
				cache1[on][pn][pv][search_category] = pops;
			} else {
				delete cache2[on][pn][pv];
				cache2[on][pn][pv] = pops;
			}
	   }
	 });
}
//--------------
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
//--------------
function toggleCh(id) {
	jQuery('tr[id^="'+id+'"]').toggle();
	return false;
}
//--------------
function servHide() {
	jQuery('tr[id^="serv"]').hide();
}
//--------------
//function UpOptions(parel,parval) {
	//jQuery('option[parel="'+parel+'"][parval="'+parval+'"]').show();
	//return false;
//}





