var LANG = "EN";
var ALLREGION = "";
var SELECTREGION = "";
var ALLCITIES = "";
var SELECTCITIES = "";
var ALLAREA = "";
var SELECTAREAS = "";
var ALLADDRESSES = "";
var SELECTADDRESSES = "";
var ADDRESSBOOKADD = "";
var ADDRESSBOOKREMOVE = "";
var GolbName = "";
var GolbScript = "";
var GlobId = "";
var div = "";
var MENU_FOCUSED = false;
var div2 = "";
var MENU_FOCUSED2 = false;
var SIZE = "";

function ShowDivs(code){
	if(document.getElementById(code).style.visibility == 'hidden'){
		document.getElementById(code).style.visibility = 'visible';
		document.getElementById(code).style.display = 'block';
		document.getElementById('img_'+code).src='/images/yelarrow2.gif';
	}else{
		document.getElementById(code).style.visibility = 'hidden';
		document.getElementById(code).style.display = 'none';
		document.getElementById('img_'+code).src='/images/yelarrow2.gif';
	}
}


ARRAY = new Array();

var NUM = 0;
var CAT_ID = 0;
function setSize(size,cat_id){
	SIZE = size;
	CAT_ID = cat_id;
	ARRAY[CAT_ID] = new Array(SIZE);
}

function addToArray(cat_id,id,en_cat,fr_cat,lurl){
	if(id != '' && en_cat != '' && fr_cat && lurl != ''){
		ARRAY[CAT_ID][NUM] = id+','+en_cat+','+fr_cat+','+lurl;
		NUM = NUM+1;
	}
}

function clearAll(){
	document.editAdd.id1.value = '';
	document.editAdd.EN_linkName1.value = '';
	document.editAdd.FR_linkName1.value ='';
	document.editAdd.link1.value = '';
	document.editAdd.id2.value = '';
	document.editAdd.EN_linkName2.value = '';
	document.editAdd.FR_linkName2.value ='';
	document.editAdd.link2.value = '';
	document.editAdd.id3.value = '';
	document.editAdd.EN_linkName3.value = '';
	document.editAdd.FR_linkName3.value ='';
	document.editAdd.link3.value = '';

}


function ValidateLinks(){
	var pattern = /^([a-zA-Z0-9_-])+(\.[a-zA-Z]+)+(\.[a-zA-Z]+)*$/;
	var enName = document.editAdd.EN_linkName1.value
	var frName = document.editAdd.FR_linkName1.value
	var links = document.editAdd.link1.value;
	if(enName == ''){
		alert('please insert English link name');
		document.editAdd.EN_linkName1.focus();
		return false;
	}
	if(frName == ''){
		alert('please insert French link name');
		document.editAdd.FR_linkName1.focus();
		return false;
	}
	if(links == ''){
		alert('please insert A valid URL ex \n www.pajesjaunes.com.lb');
		document.editAdd.link1.focus();
		return false;
	}
	flag = links.match(pattern);
	if(!flag){
		alert('please insert A valid URL ex \n www.pajesjaunes.com.lb');
		document.editAdd.link1.focus();
		return false;
	}

	var enName2 = document.editAdd.EN_linkName2.value
	var frName2 = document.editAdd.FR_linkName2.value
	var links2 = document.editAdd.link2.value;
	found1 = 0;
	if(enName2 != ''){
		found1 = 1;
	}
	if(frName2 != ''){
		found1 = 1;
	}
	if(links2 != ''){
		found1 = 1;
	}

	if(found1 == 1){
		if(enName2 == ''){
			alert('please insert English link name');
			document.editAdd.EN_linkName2.focus();
			return false;
		}
		if(frName2 == ''){
			alert('please insert French link name');
			document.editAdd.FR_linkName2.focus();
			return false;
		}
		if(links2 == ''){
			alert('please insert A valid URL ex \n www.pajesjaunes.com.lb');
			document.editAdd.link2.focus();
			return false;
		}
		flag2 = links2.match(pattern);
		if(!flag2){
			alert('please insert A valid URL ex \n www.pajesjaunes.com.lb');
			document.editAdd.link2.focus();
			return false;
		}
	}
	var enName3 = document.editAdd.EN_linkName3.value
	var frName3 = document.editAdd.FR_linkName3.value
	var links3 = document.editAdd.link3.value;
	found2 = 0;
	if(enName3 != ''){
		found2 = 1;
	}
	if(frName3 != ''){
		found2 = 1;
	}
	if(links3 != ''){
		found2 = 1;
	}

	if(found2 == 1){
		if(enName3 == ''){
			alert('please insert English link name');
			document.editAdd.EN_linkName3.focus();
			return false;
		}
		if(frName3 == ''){
			alert('please insert French link name');
			document.editAdd.FR_linkName3.focus();
			return false;
		}
		if(links3 == ''){
			alert('please insert A valid URL ex \n www.pajesjaunes.com.lb');
			document.editAdd.link3.focus();
			return false;
		}
		flag3 = links3.match(pattern);
		if(!flag3){
			alert('please insert A valid URL ex \n www.pajesjaunes.com.lb');
			document.editAdd.link3.focus();
			return false;
		}
	}

}



function listLinks(){
	var id = document.editAdd.cat[document.editAdd.cat.selectedIndex].value;
	param = '?id='+id;
	makeRequest2('/xml/links.php',param);
	if(id == ''){
		document.getElementById('linksForm').style.visibility = 'hidden';
		document.getElementById('linksForm').style.display = 'none';
	}else{
		document.getElementById('linksForm').style.visibility = 'visible';
		document.getElementById('linksForm').style.display = 'block';
	}
}


isIE=document.all;
isNN=!document.all&&document.getElementById;
isN4=document.layers;
isHot=false;

function ddInit(e){
	topDog=isIE ? "BODY" : "HTML";
	whichDog=isIE ? document.all.theLayer : document.getElementById("theLayer");
	hotDog=isIE ? event.srcElement : e.target;
	while (hotDog.id!="titleBar"&&hotDog.tagName!=topDog){
		hotDog=isIE ? hotDog.parentElement : hotDog.parentNode;
	}
	if (hotDog.id=="titleBar"){
		offsetx=isIE ? event.clientX : e.clientX;
		offsety=isIE ? event.clientY : e.clientY;
		nowX=parseInt(whichDog.style.left);
		nowY=parseInt(whichDog.style.top);
		ddEnabled=true;
		document.onmousemove=dd;
	}
}

function dd(e){
	if (!ddEnabled) return;
	whichDog.style.left=isIE ? nowX+event.clientX-offsetx : nowX+e.clientX-offsetx;
	whichDog.style.top=isIE ? nowY+event.clientY-offsety : nowY+e.clientY-offsety;
	return false;
}

function ddN4(whatDog){
	if (!isN4) return;
	N4=eval(whatDog);
	N4.captureEvents(Event.MOUSEDOWN|Event.MOUSEUP);
	N4.onmousedown=function(e){
		N4.captureEvents(Event.MOUSEMOVE);
		N4x=e.x;
		N4y=e.y;
	}
	N4.onmousemove=function(e){
		if (isHot){
			N4.moveBy(e.x-N4x,e.y-N4y);
			return false;
		}
	}
	N4.onmouseup=function(){
		N4.releaseEvents(Event.MOUSEMOVE);
	}
}

function hideMe(){
	if (isIE||isNN) whichDog.style.visibility="hidden";
	else if (isN4) document.theLayer.visibility="hide";
}

function showMe(){
	if (isIE||isNN) whichDog.style.visibility="visible";
	else if (isN4) document.theLayer.visibility="show";
	document.getElementById("titleLayer").innerHTML = "Add Contact Information";
	document.contact.english_category.value = '';
	document.contact.french_category.value = '';
	document.contact.id.value = '';
	document.contact.website.value = '';
	document.contact.email.value = '';
	document.contact.phone.value = '';
	document.contact.phone2.value = '';
	document.contact.phone3.value = '';
}

function showMe2(id,fr_category_name,category_name,website,link_address,phone_number1,phone_number2,phone_number3){
	if (isIE||isNN) whichDog.style.visibility="visible";
	else if (isN4) document.theLayer.visibility="show";
	document.getElementById("titleLayer").innerHTML = "Edit Contact Information";
	document.contact.english_category.value = category_name;
	document.contact.french_category.value = fr_category_name;
	document.contact.id.value = id;
	document.contact.website.value = website;
	document.contact.email.value = link_address;
	document.contact.phone.value = phone_number1;
	document.contact.phone2.value = phone_number2;
	document.contact.phone3.value = phone_number3;
}

function showMe3(id,fr_category_name,category_name){
	whichDog.style.left = "-130";
	whichDog.style.top = "240";
	if (isIE||isNN) whichDog.style.visibility="visible";
	else if (isN4) document.theLayer.visibility="show";
	if(id == '' && fr_category_name == '' && category_name == ''){
		document.getElementById("titleLayer").innerHTML = "Add Link Categories";
	}else{
		document.getElementById("titleLayer").innerHTML = "Edit Link Categories";
	}
	document.cat.english_category.value = category_name;
	document.cat.french_category.value = fr_category_name;
	document.cat.id.value = id;
}

function showMe4(id,fr_title,en_title,fr_text,en_text){
	whichDog.style.left = "-130";
	whichDog.style.top = "240";
	if (isIE||isNN) whichDog.style.visibility="visible";
	else if (isN4) document.theLayer.visibility="show";
	if(id == '' && fr_title== '' && en_title== '' && fr_text== '' && en_text== ''){
		document.getElementById("titleLayer").innerHTML = "Add Product";
		document.getElementById("logoLayer").innerHTML = "";
	}else{
		document.getElementById("titleLayer").innerHTML = "Edit Product";
		document.getElementById("logoLayer").innerHTML = "In case you want to keep the same image, leave the logo field empty";
	}
	document.prod.french_title.value = fr_title;
	document.prod.english_title.value = en_title;
	document.prod.english_text.value = en_text;
	document.prod.french_text.value = fr_text;
	document.prod.id.value = id;
}


function showMe5(id,fr_text,en_text){
	whichDog.style.left = "-130";
	whichDog.style.top = "240";
	if (isIE||isNN) whichDog.style.visibility="visible";
	else if (isN4) document.theLayer.visibility="show";
	if(id == '' && fr_text== '' && en_text== ''){
		document.getElementById("titleLayer").innerHTML = "Add Announcement";
	}else{
		document.getElementById("titleLayer").innerHTML = "Edit Announcement";
	}
	document.announ.english_text.value = en_text;
	document.announ.french_text.value = fr_text;
	document.announ.id.value = id;
}


function validateAddProduct(){
	var root=document.prod;

	if(root.english_title.value==''){
		alert('Please provide an english title');
		root.english_title.focus();
		return false;
	}
	if(root.english_text.value==''){
		alert('Please provide an english text');
		root.english_text.focus();
		return false;
	}
	if(root.french_title.value==''){
		alert('Please provide an french title');
		root.french_title.focus();
		return false;
	}
	if(root.french_text.value==''){
		alert('Please provide an french text');
		root.french_text.focus();
		return false;
	}
	if(root.id.value==''){
		if(root.logo.value==''){
			alert('Please browse an image.');
			root.logo.focus();
			return false;
		}
	}

	return true;
}

function validateAddAnnouncement(){
	var root=document.announ;

	if(root.english_text.value==''){
		alert('Please provide an english text');
		root.english_text.focus();
		return false;
	}

	if(root.french_text.value==''){
		alert('Please provide an french text');
		root.french_text.focus();
		return false;
	}

	return true;
}

document.onmousedown=ddInit;
document.onmouseup=Function("ddEnabled=false");

function validateLinkCategory(){
	enC =  document.cat.english_category.value
	frC =  document.cat.french_category.value
	if(enC == ""){
		alert('Please insert ENGLISH category link');
		document.cat.english_category.focus();
		return false;
	}
	if(frC == ''){
		alert('Please insert FRENCH category link');
		document.cat.french_category.focus();
		return false;
	}
}

function validateContactInfo(){
	var root = document.contact;
	var english_category  = root.english_category.value;
	if(english_category == ''){
		alert('Please insert english category');
		root.english_category.focus();
		return false;
	}
	var french_category  = root.french_category.value;
	if(french_category == ''){
		alert('Please insert french category');
		root.french_category.focus();
		return false;
	}
	var phone = root.phone.value;
	if(phone == ''){
		alert('Please insert phone number');
		root.phone.focus();
		return false;
	}



}

function validateAboutUs(){
	var root = document.about;
	var Estblished  = root.established.value;
	if(Estblished == ''){
		alert("Please insert the year your company was established in.");
		root.established.focus();
		return false;
	}
	var legal  = root.legal.value;
	if(legal == ''){
		alert("Please insert your company legal status example \n \'SARL SAL LTD...\'");
		root.legal.focus();
		return false;
	}
	var capital  = root.capital.value;
	if(capital == ''){
		alert("Please insert your company capital example \n \'20 000 000 LL...\'");
		root.capital.focus();
		return false;
	}
	var num  = root.num_of_employees.value;
	if(num == ''){
		alert("Please insert your company number of employees.");
		root.num_of_employees.focus();
		return false;
	}
	var manager  = root.manager.value;
	if(manager == ''){
		alert("Please insert the company Nanager Name.");
		root.manager.focus();
		return false;
	}
	var sector  = root.sector.value;
	if(sector == ''){
		alert("Please insert your company sector.");
		root.sector.focus();
		return false;
	}

}

function passValidation(){
	var root = document.change_password;
	var oldp = root.old.value;
	var newp = root.newp.value;
	var confnew = root.confnew.value;

	if (oldp == '' || newp == '' || confnew == ''){
		alert('Please fill up all the fields');
		root.old.focus();
		return false;
	}
	if (!isNaN(newp)){
		alert('Please insert a password that contains characters.');
		root.newp.focus();
		return false;
	}

	if (newp != confnew){
		alert('The password and it\'s confirmation do not match');
		root.confnew.focus();
		return false;
	}

	oldp = hex_md5(oldp);
	newp = hex_md5(newp);
	confnew = hex_md5(confnew);

	root.old.value = oldp;
	root.newp.value = newp;
	root.confnew.value = confnew;
	root.enc.value = '1';

	return true;
}


function showLayer(lay,flag){
	if(lay == 'established2'){
		text = 'Please insert the year your company was established in.';
	}else if(lay == 'legal2'){
		text = 'Please insert your company legal status<br> example: <B>SARL, SAL, LTD</B>';
	}
	else if(lay == "capital2"){
		text = 'Please insert your company capital example 20 000 000 LL';
	}else if(lay == "num2"){
		text = "Please insert your company number of employees.";
	}else if(lay == "manager2"){
		text = "Please insert the company Nanager Name.";
	}else if(lay == "sector2"){
		text = "Please insert your company sector.";
	}else if(lay == "old2"){
		text = "Please insert your old password.";
	}else if(lay == "new2"){
		text = "Please insert your New Password.";
	}
	else if(lay == "confnew2"){
		text = "Please retype your new password.";
	}
	else if(lay == "encat2"){
		text = "Please insert/change english category name.<BR>(OBLIGATORY FIELD)";
	}
	else if(lay == "frcat2"){
		text = "Please insert/change french category name.<BR>(OBLIGATORY FIELD)";
	}
	else if(lay == "web2"){
		text = "Please insert/change Website Field.<BR>(NON OBLIGATORY FIELD)";
	}
	else if(lay == "email2"){
		text = "Please insert/change E-mail Filed.<BR>(NON OBLIGATORY FIELD)";
	}
	else if(lay == "fone12"){
		text = "Please insert/change first Phone Filed.<BR>(OBLIGATORY FIELD)";
	}
	else if(lay == "fone22"){
		text = "Please insert/change second Phone Filed.<BR>(NON OBLIGATORY FIELD)";
	}
	else if(lay == "fone32"){
		text = "Please insert/change third Phone Filed.<BR>(NON OBLIGATORY FIELD)";
	}else if(lay == "flink21-1"){
		text = "Please insert/change first english Link Filed.<BR>(OBLIGATORY FIELD) EX: Yellow Pages";
	}else if(lay == "flink21-2"){
		text = "Please insert/change first French Link Filed.<BR>(OBLIGATORY FIELD) EX: Pages Jaunes";
	}else if(lay == "flink21-3"){
		text = "Please insert/change Link Filed.<BR>(OBLIGATORY FIELD) EX: www.pagesjaunes.com <BR>";
	}
	else if(lay == "flink22-1"){
		text = "Please insert/change Second english Link Filed.<BR>(NON OBLIGATORY FIELD) EX: Yellow Pages";
	}else if(lay == "flink22-2"){
		text = "Please insert/change Second French Link Filed.<BR>(NON OBLIGATORY FIELD) EX: Pages Jaunes";
	}else if(lay == "flink22-3"){
		text = "Please insert/change Link Filed.<BR>(NON OBLIGATORY FIELD) EX: www.pagesjaunes.com <BR>";
	}

	else if(lay == "flink23-1"){
		text = "Please insert/change Third english Link Filed.<BR>(NON OBLIGATORY FIELD) EX: Yellow Pages";
	}else if(lay == "flink23-2"){
		text = "Please insert/change Third French Link Filed.<BR>(NON OBLIGATORY FIELD) EX: Pages Jaunes";
	}else if(lay == "flink23-3"){
		text = "Please insert/change Link Filed.<BR>(NON OBLIGATORY FIELD) EX: www.pagesjaunes.com <BR>";
	}else if(lay == "logo"){
		text = "Click on the browse button to upload <br>your company logo from local PC.<BR>(OBLIGATORY FIELD)";
	}else if(lay == "enti"){
		text = "Please specify an english title.<BR>(OBLIGATORY FIELD)";
	}else if(lay == "frti"){
		text = "Please specify a french title.<BR>(OBLIGATORY FIELD)";
	}else if(lay == "ente"){
		text = "Please specify an english text.<BR>(OBLIGATORY FIELD)";
	}else if(lay == "frte"){
		text = "Please specify a french text.<BR>(OBLIGATORY FIELD)";
	}else if(lay == "p_logo"){
		text = "Please browse a logo.<BR>(OBLIGATORY FIELD)";
	}
	else if(lay =='company_name_en'){
		text ="Please specify the Company Name in English";
	}else if(lay =='company_name_fr'){
		text ="Please specify the Company Name in French";
	}
	if(flag == 1){
		vis = "visible";
		eval("document.getElementById('"+lay+"_text').innerHTML='"+text+"';");
		eval("document.getElementById('"+lay+"_container').style.visibility = '"+vis+"';");
	}else{
		text = "";
		vis = "hidden";
		eval("document.getElementById('"+lay+"_text').innerHTML='"+text+"';");
		eval("document.getElementById('"+lay+"_container').style.visibility = '"+vis+"';");
	}



}




function setLang(lang){

	LANG = lang;

	if(LANG =="EN" || LANG =="en"){
		ALLCITIES = "You have selected All Regions";
		SELECTCITIES = "Please Select a Region from Above";
		ALLAREA = "You have selected All Areas";
		SELECTAREAS = "Please Select a City from Above";
		ALLADDRESSES = "You have selected All Addresses";
		SELECTADDRESSES = "Please Select an Area from Above";
		ADDRESSBOOKREMOVE = "Remove from your addressbook";
		ADDRESSBOOKADD = "Add to your addressbook";
	}else{
	    if(LANG=="FR" || LANG == "fr"){
		ALLCITIES = "Vouz avez choisis toutes les villes";
		SELECTCITIES = "Choisir une categorie du dessus";
		ALLAREA = "Vouz avez choisis toutes les Secteurs";
		SELECTAREAS = "Choisir une ville du dessus";
		ALLADDRESSES = "Vouz avez choisis toutes les Adresses";
		SELECTADDRESSES = "Choisir un secteur du dessus";
		ADDRESSBOOKREMOVE = "Supprimer de mon carnet d'adresses";
		ADDRESSBOOKADD = "Ajouter à mon carnet d'adresses";
	    }else{
		ALLCITIES = "&#1604;&#1602;&#1583; &#1575;&#1582;&#1578;&#1585;&#1578; &#1603;&#1604; &#1575;&#1604;&#1605;&#1583;&#1606;";
		SELECTCITIES = "&#1575;&#1604;&#1585;&#1580;&#1575;&#1569; &#1573;&#1582;&#1578;&#1610;&#1575;&#1585; &#1575;&#1604;&#1605;&#1606;&#1591;&#1602;&#1577;";
		ALLAREA = "&#1604;&#1602;&#1583; &#1575;&#1582;&#1578;&#1585;&#1578; &#1580;&#1605;&#1610;&#1593; &#1575;&#1604;&#1605;&#1606;&#1575;&#1591;&#1602;";
		SELECTAREAS = "&#1575;&#1604;&#1585;&#1580;&#1575;&#1569; &#1573;&#1582;&#1578;&#1610;&#1575;&#1585; &#1575;&#1604;&#1605;&#1606;&#1591;&#1602;&#1577;";
		ALLADDRESSES = "&#1604;&#1602;&#1583; &#1575;&#1582;&#1578;&#1585;&#1578; &#1580;&#1605;&#1610;&#1593; &#1575;&#1604;&#1593;&#1606;&#1575;&#1608;&#1610;&#1606;";
		SELECTADDRESSES = "&#1575;&#1604;&#1585;&#1580;&#1575;&#1569; &#1573;&#1582;&#1578;&#1610;&#1575;&#1585; &#1575;&#1604;&#1605;&#1583;&#1610;&#1606;&#1577;";
		ADDRESSBOOKREMOVE = "حذف من الدليل الشخصي";
		ADDRESSBOOKADD = "اضف للدليل الشخصي";
	    }
	}

}

function invalid(){
	document.asform.active.value = '0';
}

function onBusinessChange(){
	
	document.asform.activity.value = '';
	document.asform.sub_activity.value = '';
	document.asform.sub_activity_value.value = '';
	document.asform.active.value = '0';
	document.asform.businesstext.value = '';
}

function levelTwo(Activity,name){
	//var bs = window.opener.document.asform.business.value = name;
	var bs = window.opener.document.asform.business.value = '';
	var hf2 =window.opener.document.asform.activity.value = Activity;
	var hf3 =window.opener.document.asform.sub_activity.value = '';
	var hf4 =window.opener.document.asform.sub_activity_value.value = '';
	var hf4 =window.opener.document.asform.active.value = '1';
	window.opener.document.asform.businesstext.value = name;
	window.opener.document.asform.search.focus();
	self.close();
}

function levelThree(activityName,ActId,name,subActivity){
	//var bs = window.opener.document.asform.business.value = activityName+'|'+name;
	var bs = window.opener.document.asform.business.value = '';
	var hf2 = window.opener.document.asform.activity.value = ActId;
	var hf3 = window.opener.document.asform.sub_activity.value = subActivity;
	var hf4 = window.opener.document.asform.sub_activity_value.value = '';
	var hf4 =window.opener.document.asform.active.value = '1';
	window.opener.document.asform.businesstext.value = activityName + ' / ' + name;
	window.opener.document.asform.search.focus();
	self.close();
}

function levelFour(activityName,ActId,name,subActivity,valuesr,valueCode){
	//var bs =window.opener.document.asform.business.value = activityName+'|'+name+'|'+valuesr;
	var bs =window.opener.document.asform.business.value = '';
	var hf2 =window.opener.document.asform.activity.value = ActId;
	var hf3 =window.opener.document.asform.sub_activity.value = subActivity;
	var hf4 =window.opener.document.asform.sub_activity_value.value = valueCode;
	var hf4 =window.opener.document.asform.active.value = '1';
	window.opener.document.asform.businesstext.value = activityName + ' / ' + name + ' / ' + valuesr;
	window.opener.document.asform.search.focus();
	self.close();
}


function shutDiv(){
	d = div;
	showLay(d,0);
}

function shutDiv2(){
	d = div2;
	showLay2(d,0);
}



function Delay(lay){
	MENU_FOCUSED = false;
	div = lay;
	setTimeout("shutDiv()",500);
	return;
}

function Delay2(lay){
	MENU_FOCUSED2 = false;
	div2 = lay;
	setTimeout("shutDiv2()",500);
	return;
}

function setMenuFocused() {
	MENU_FOCUSED = true;
}

function setMenuFocused2() {
	MENU_FOCUSED2 = true;
}


function showLay(lay,fl){
	if(fl == 1){
		setMenuFocused()
		vis = 'visible';
		document.getElementById(lay).style.visibility = vis;
	}else{
		vis = 'hidden';
		if (!MENU_FOCUSED){

			document.getElementById(lay).style.height = '1';
			document.getElementById(lay).style.visibility = vis;
		}
	}
}


function showLay2(lay,fl){
	if(fl == 1){
		setMenuFocused2()
		vis = 'visible';
		document.getElementById(lay).style.visibility = vis;
	}else{
		vis = 'hidden';
		if (!MENU_FOCUSED2){
			document.getElementById(lay).style.visibility = vis;
		}
	}
}



function ShowEx(flag){

	if(flag == 1){
		vis = 'visible';
	}else{
		vis = 'hidden';
	}
	document.getElementById('example').style.visibility = vis;
}

function ShowEx2(flag){

	if(flag == 1){
		vis = 'visible';
	}else{
		vis = 'hidden';
	}
	document.getElementById('example2').style.visibility = vis;
}


function validateQuickSearch(){
	if (document.qsform.q.value == '') return false;
	if (!isNaN(document.qsform.q.value)) {
		window.alert('Searching for phone numbers is not allowed.\nPlease update your search keyword and try again.');
		return false;
    }
	return true;
}

function validateAdvancedSearch(){
	if (document.asform.search.value == '') {
		if ( ( (document.asform.business.value == '') && (document.asform.activity.value == '') ) || (document.asform.regions[document.asform.regions.selectedIndex].value == 'all') ){
			window.alert('You must either provide a business name or select a sector and a region');
			return false;
		}
	}
	document.asform.businesstext.disabled = false;
	return true;
}

function makeRequest(url, parameters,name,scrit,theId) {

	GolbName = name;
	GolbScript = scrit;
	GlobId = theId;
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = alertContents;
	http_request.open('GET', url + parameters, true);
	http_request.send(null);
}


function makeRequest2(url, parameters) {

	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = alertContents2;
	http_request.open('GET', url + parameters, true);
	http_request.send(null);
}


function alertContents2() {


	if (http_request.readyState == 4) {
		if (http_request.status == 200) {

			xmldoc = http_request.responseXML;
			ro = xmldoc.getElementsByTagName('cat').item(0);
			len = ro.childNodes.length;

			var id =  ro.childNodes.item(1).childNodes.item(0).data;
			var enName =  ro.childNodes.item(3).childNodes.item(0).data;
			var frName =  ro.childNodes.item(5).childNodes.item(0).data;
			var url1 =  ro.childNodes.item(7).childNodes.item(0).data;
			if(id == 'empty') id = '';
			if(enName == 'empty') enName = '';
			if(frName == 'empty') frName = '';
			if(url1 == 'empty') url1 = '';



			document.editAdd.id1.value = id;
			document.editAdd.FR_linkName1.value = frName;
			document.editAdd.EN_linkName1.value = enName;
			document.editAdd.link1.value = url1;


			var id2 =  ro.childNodes.item(9).childNodes.item(0).data;
			var enName2 =  ro.childNodes.item(11).childNodes.item(0).data;
			var frName2 =  ro.childNodes.item(13).childNodes.item(0).data;
			var url2 =  ro.childNodes.item(15).childNodes.item(0).data;
			if(id2 == 'empty') id2 = '';
			if(enName2 == 'empty') enName2 = '';
			if(frName2 == 'empty') frName2 = '';
			if(url2 == 'empty') url2 = '';
			document.editAdd.id2.value = id2;
			document.editAdd.FR_linkName2.value = frName2;
			document.editAdd.EN_linkName2.value = enName2;
			document.editAdd.link2.value = url2;

			var id3 =  ro.childNodes.item(17).childNodes.item(0).data;
			var enName3 =  ro.childNodes.item(19).childNodes.item(0).data;
			var frName3 =  ro.childNodes.item(21).childNodes.item(0).data;
			var url3 =  ro.childNodes.item(23).childNodes.item(0).data;
			if(id3 == 'empty') id3 = '';
			if(enName3 == 'empty') enName3 = '';
			if(frName3 == 'empty') frName3 = '';
			if(url3 == 'empty') url3 = '';
			document.editAdd.id3.value = id3;
			document.editAdd.FR_linkName3.value = frName3;
			document.editAdd.EN_linkName3.value = enName3;
			document.editAdd.link3.value = url3;
			document.getElementById("DIV_IMG").innerHTML = "";

		}else{
			alert('There was a problem with the request.');
		}
	}
	else{
		document.getElementById("DIV_IMG").innerHTML = "<img src=\"/images/loading.gif\" align=\"absmiddle\">&nbsp;loading ... ";

	}
}


function alertContents() {


	if (http_request.readyState == 4) {
		if (http_request.status == 200) {

			xmldoc = http_request.responseXML;
			ro = xmldoc.getElementsByTagName('msg').item(0);
			len = ro.childNodes.length;
			data = "";

			for (var iNode = 0; iNode < len; iNode++) {
				if(GlobId == ro.childNodes.item(iNode).childNodes.item(0).data){
					as = 'selected';
				}else{
					as = '';
				}
				data += '<option value=\"'+ro.childNodes.item(iNode).childNodes.item(0).data+'\" '+as+'>'
				data += ro.childNodes.item(iNode+1).childNodes.item(0).data +'</option>';
				iNode++
			}
			//alert(GolbName);
			// all = "<select name=\""+GolbName+"\" class=\"asselectfield\" onchange=\""+GolbScript+"\"><option value=\"\">Select Below</option>" + data + "</select>";
			all = "<select name=\""+GolbName+"\" class=\"asselectfield\" onchange=\""+GolbScript+"\">" + data + "</select>";

			eval('document.getElementById("'+GolbName+'").innerHTML =\''+all+'\';');

		}else{
			alert('There was a problem with the request.');
		}
	}
	else{
		eval('document.getElementById("'+GolbName+'").innerHTML = "loading ... ";');
	}
}


function displayCities(cityId){
	if(cityId == undefined){
		cityId = '';
	}

	id = document.asform.regions[document.asform.regions.selectedIndex].value;
	
	if(id == ''){
		document.getElementById("cities").innerHTML = ALLCITIES;

	}else if(id == 'all'){
		document.getElementById("cities").innerHTML = ALLCITIES;
	}
	else{
		param = '?id='+id;
		makeRequest('/xml/cities.php',param,"cities","doCityChange(); return ;",cityId);
	}
}

function doCityChange(){
    $('#asform input[name=cities_text]').val($('#asform select[name=cities] option:selected').html());
}

function openCat(){
	window.open('/categories.php','Categories','width=600,height=500,scrollbars=1,menubars=0,left=580,top=120');
	return false;
}

function checkLogo(){
	var root = document.logo;
	var file = root.new_logo;
	if(file.value==''){
		alert("Please Browse for a new logo image before you continue");
		file.focus();
		return false;
	}
	return true;
}
function validateCompanyInfo(){
	var root = document.welcome_company_info;
	var en_name = root.en_name;
	var fr_name = root.en_name;
	
	if(en_name.value==''){
		alert("Please enter the company name in English");
		en_name.focus();
		return false;
	}
	if(fr_name.value==''){
		alert("Please enter the company name in French");
		fr_name.focus();
		return false;
	}
	return true;
}


function replaceBusinessAndSubmit(businessName){
	root=document.AS;
	
	root.business.value=businessName;
	root.submit();
}
function resetAdvancedSearch() {
	document.asform.businesstext.value = '';
	document.asform.activity.value = '';
	document.asform.sub_activity.value = '';
	document.asform.sub_activity_value.value = '';
	document.asform.search.value = '';
	document.asform.business.value = '';
	document.asform.regions.selectedIndex = 0;
	document.asform.withemail[0].checked = true;
	document.asform.withwebsite[0].checked = true;
	
	// update the cities combo after region selection
	displayCities();
}
function clearSelectedGuideValues() {
	if (document.asform.business.value != '') {
		document.asform.businesstext.value = '';
		document.asform.activity.value = '';
		document.asform.sub_activity.value = '';
		document.asform.sub_activity_value.value = '';
	}
}

function addToAddressBook(id){
    $.ajax({
	url:"/users/address_book_function.php",
	data: {method:"add", subscriber_code: id},
	type: "POST",
	dataType: "json",
	success: function(data){
	    if(data == null){
		return;
	    }
	    if(data.login){
		$("#use_addressbook_text").show();
		$("#hidden_login_link").click();
		return;
	    }
	    if(data.success){
		var obj = "#abspan_"+id;
		$(obj).removeClass("sritemaddbook");
		$(obj).removeAttr('onclick').unbind("click");
		$(obj).click( function(){
		    deleteFromAddressBook(this,id,name,false);});
		$(obj).addClass("sriteminbook");
		$(obj).attr("title", ADDRESSBOOKREMOVE);
		$(obj).tipsy();
		$(obj).tipsy("hide");
		$(obj).tipsy("show");
	    }
	}});    
}

function deleteFromAddressBook(id, refreshpage){
    $.ajax({
	url:"/users/address_book_function.php",
	data: {method:"delete", subscriber_code: id},
	type: "POST",
	dataType: "json",
	success: function(data){
	    if(data == null){
		return;
	    }
	    if(data.login){
		$("#use_addressbook_text").show();
		$("#hidden_login_link").click();
		return;
	    }
	    if(data.success){
		if(!refreshpage){
		    var obj = "#abspan_"+id;
		    $(obj).removeClass("sriteminbook");
		    $(obj).removeAttr('onclick').unbind("click");
		    $(obj).click( function(){addToAddressBook(id);});
		    $(obj).addClass("sritemaddbook");
		    $(obj).attr("title", ADDRESSBOOKADD);
		    $(obj).tipsy();
		    $(obj).tipsy("hide");
		    $(obj).tipsy("show");
		}else{
		    window.location = window.location;
		}
	    }
	}});    
}

$(document).ready(function(){
    $("a.fancy_link").fancybox();
    $("#login_form").submit(function(e){
	login("email");
	return false;
    });
    $("#signup_form").submit(function(e){
	signup("email");
	return false;
    });
    var inputs = $('div.input_field input[type="text"], div.input_field input[type="password"]');
    
    // remove label on focus
    inputs.focus(function(){
	$('label[for="'+$(this).attr('id')+'"]').hide();
    });
    inputs.blur(function(){
	var that = $(this);
	if (that.val() == ""){
	    $('label[for="'+that.attr('id')+'"]').show();
	}
    });
    inputs.blur();
});
var login_action = "";
var login_type = "";
var login_code = "";

function login(type){
    $("#login_error").hide();
    var data = {type: type};
    if(type == "email"){
	data.method = "login";
	var empty= false;
	$("#login_form input").each(function(){
	    if($(this).val() == ""){
		empty = true;
	    }});
	if(empty){
	    $("#login_error").html("Both email and password are required");
	    $("#login_error").show();
	    return;
	}	
	if(!validateEmail($("#login_email").val())){
	    $("#login_error").html("Email is not proper");
	    $("#login_error").show();
	    return false;
	}
	data.email = $("#login_email").val();
	data.password = $("#login_password").val();
	data.post_action = login_action;
	data.post_type = login_type;
	data.post_code = login_code;
    }
    if(type == "facebook"){
	data.step = "initiate";
    }
    $.ajax({
	url: "/users/login.php",
	data: data,
	dataType: "json",
	success: function(data){
	    if(data.success){
		if(data.redirect != "" && data.redirect != null){
		    window.location.href = data.redirect;
		}else{
		    var href_additions = "";
		    if(data.action == "rate"){
			href_additions = "act=rate&type=" + data.rate + "&code=" +data.code;
		    }
		    if(href_additions != ""){
			if(window.location.search != ""){
			    href_additions = "&" + href_additions;
			}else{
			    href_additions = "?" + href_additions;
			}
		    }
		    var href =  window.location.href + href_additions;
		    href = href.replace('#','');
		    window.location.href = href;
		}
	    }else{
		$.fancybox({content: "<div class='error'>"+data.error+"</div>"});
	    }
	},
	error: function(){
	    $.fancybox({content: "<div class='error'>An error has occurred please try later</div>"});
	}
    });
}

function signup(type){
    $("#signup_error").hide();
    var data = { type: type};
    if(type == "email"){
	var empty= false;
	data.method = "signup";
	$("#signup_form input.required").each(function(){
	    if($(this).val() == ""){
		empty = true;
	    }});
	if(empty){
	    $("#signup_error").html("All required fields must be filled");
	    $("#signup_error").show();
	    return false;
	}
	if(!validateEmail($("#signup_email").val())){
	    $("#signup_error").html("Email is not proper");
	    $("#signup_error").show();
	    return false;
	}		
	if( $("#signup_password").val() != $("#signup_repeat_password").val()){
	    $("#signup_error").html("Passwords do not match");
	    $("#signup_error").show();
	    return false;
	}
	data.email = $("#signup_email").val();
	data.password = $("#signup_password").val();
	data.company = $("#signup_company").val();
	data.position = $("#signup_position").val();
	data.name = $("#signup_name").val();
	data.post_action = login_action;
	data.post_type = login_type;
	data.post_code = login_code;
    }
    if(type == "facebook"){
	data.step = "initiate";
    }	    
    $.ajax({
	url: "/users/login.php",
	data: data,
	dataType: "json",
	success: function(data){
	    if(data.success){
		if(data.redirect != "" && data.redirect != null){
		    window.location.href = data.redirect;
		}else{
		    var href_additions = "";
		    if(data.action == "rate"){
			href_additions = "act=rate&type=" + data.rate + "&code=" +data.code;
		    }
		    if(href_additions != ""){
			if(window.location.search != ""){
			    href_additions = "&" + href_additions;
			}else{
			    href_additions = "?" + href_additions;
			}
		    }
		    var href =  window.location.href + href_additions;
		    href = href.replace('#','');
		    window.location.href = href;
		}
	    }else{
		$.fancybox({content: "<div class='error'>"+data.error+"</div>"});
	    }
	},
	error: function(){
	    $.fancybox({content: "<div class='error'>An error has occurred please try later</div>"});
	}
    });
}		


function validateEmail(elementValue){  
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
    return emailPattern.test(elementValue);  
} 


function rateKPI(rater, kpi, value){
    $("#my_rating_"+rater+"_"+kpi).css("width", value+"%");
    if(LANG == "AR" || LANG == "ar"){
	$("#my_rating_"+rater+"_"+kpi).css("left", (100 - value)+"%");
    }
    
    total_sum =0;
    total_count = 0;
    $(".my_rating_"+rater+"_kpis").each(function(){
	total_count++;
	total_sum += parseInt($(this).css("width")) * parseInt($(this).attr("factor"));
    });
    $("#my_rating_"+rater+"_overall").css("width", (total_sum/total_count) +"%");
    if(LANG == "AR" || LANG == "ar"){
	$("#my_rating_"+rater+"_overall").css("left", (100 - total_sum/total_count)+"%");
    }
}
    
function submit_my_rating(rating_code){
    data={method: $("#my_rating_"+rating_code+" input").val(), 
	  rating: rating_code,
	  subscriber: $("#subscriber_code_for_rating_"+rating_code).val(),
	  kpis: new Array(),
	  overall: parseInt($("#my_rating_"+rating_code+"_overall").css("width"))};
    $(".my_rating_"+rating_code+"_kpis").each(function(){
	data.kpis.push({id: $(this).attr("kpi_id"), value: parseInt($(this).css("width"))});
    });
    $(".my_rating_"+rating_code+"_select").each(function(){
	data.kpis.push({id: $(this).attr("kpi_id"), value: $(this).val()});
    });
    $.ajax({
	url: "/rating.php",
	data: data,
	dataType: "json",
	type: "POST",
	success: function(data){
	    if(data.success){
		$.fancybox.close();
		var index = window.location.href.indexOf("act=");
		if(index != -1){
		    window.location.href = window.location.href.substring(0, index);
		}else{
		    window.location.reload(true);
		}
	    }else{
		$.fancybox({content: "<div class='error'>"+data.error+"</div>"});
	    }
	},
	error: function(){
	    $.fancybox({content: "<div class='error'>An error has occurred please try later</div>"});
	}
    });


}

function getSubscriberRating(subscriber,rating){
    $.ajax({
	url: "/rating.php",
	data: {method: 'get_subscriber_rating', subscriber: subscriber}, 
	dataType: "json",
	type: "POST",
	success: function(data){
	    if(data.login){
		$("#use_addressbook_text").show();
		$("#hidden_login_link").click();
		return;		
	    }	    
	    if(data.success){
		for(var i=0; i<data.subs.length; i++){
		    if($("#rating_"+rating+"_"+data.subs[i].kpi_id)[0].tagName == "SELECT"){
			$("#rating_"+rating+"_"+data.subs[i].kpi_id).val(data.subs[i].kpi_value);
		    }else{
			$("#rating_"+rating+"_"+data.subs[i].kpi_id).css("width", data.subs[i].kpi_value+"%");
			if(LANG == "AR" || LANG == "ar"){
			    $("#rating_"+rating+"_"+data.subs[i].kpi_id).css("left", (100 - data.subs[i].kpi_value)+"%");
			}
		    }
		}
		$("#rating_"+rating+"_overall").css("width", data.average_rating +"%");
		if(LANG == "AR" || LANG == "ar"){
		    $("#rating_"+rating+"_overall").css("left", (100 - data.average_rating) +"%");
		}
	    }
	    $("#my_rating_"+rating).hide();
	    $("#others_rating_"+rating).show();
	    $("#rating_"+rating+"_error").hide();
	    $("#hidden_rating_"+rating+"_link").click();
	}});
    
}


function rateSubscriber(subscriber, rating){
    $("#subscriber_code_for_rating_"+rating).val(subscriber);
    $.ajax({
	url: "/rating.php",
	data: {method: 'get_user_rating', subscriber: subscriber}, 
	dataType: "json",
	type: "POST",
	success: function(data){
	    if(data.login){
		$("#use_addressbook_text").show();
		$("#hidden_login_link").click();
		login_action = "rate";
		login_type = rating;
		login_code = subscriber;
		return;		
	    }
	    
	    $("#my_rating_"+rating+" input").val('new');
	    if(data.success){
		for(var i=0; i<data.subs.length; i++){
		    if($("#my_rating_"+rating+"_"+data.subs[i].kpi_id)[0].tagName == "SELECT"){
			$("#my_rating_"+rating+"_"+data.subs[i].kpi_id).val(data.subs[i].kpi_value);
		    }else{
			$("#my_rating_"+rating+"_"+data.subs[i].kpi_id).css("width", data.subs[i].kpi_value+"%");
			if(LANG == "AR" || LANG == "ar"){
			    $("#my_rating_"+rating+"_"+data.subs[i].kpi_id).css("left", (100 - data.subs[i].kpi_value)+"%");
			}
		    }
		}
		$("#my_rating_"+rating+"_overall").css("width", data.average_rating +"%");
		if(LANG == "AR" || LANG == "ar"){
		    $("#my_rating_"+rating+"_overall").css("left", (100 - data.average_rating) +"%");
		}
		$("#my_rating_"+rating+" input").val('edit');
	    }else{
		$(".my_rating_"+rating+"_kpis").css("width", "0%");
	    }
	    $("#my_rating_"+rating).show();
	    $("#others_rating_"+rating).hide();
	    $("#rating_"+rating+"_error").hide();
	    $("#hidden_rating_"+rating+"_link").click();
	}});
    
}



