// ext_profile.php
$(document).ready(function(){
    
});

function p_hide_form(){
    $("#table_msg").hide();
    $("#table_msg_confirmation").show();
}
function p_show_msg(str){
    // str = xml Response
    //$("#table_msg_confirmation span").text(str);
    $("#table_msg_confirmation").show();
}


function p_hide_add_contact(){
    $("#div_ajouter_contact").hide();
    
}
function p_show_contact_msg(str){
    $("#div_ajouter_contact_confirmation").show();
    //$("#div_ajouter_contact_confirmation span").text(str);
}

// re ecriture de "funct_client.js"
// pourrait remplacer les fct ajax ....
// si ya un bug

//clearing message field value
//remebers if the uses has already clicked on the message field
var cleared=false;


//this functions sends or deletes the message
function p_send_msg(msg_id, to_user_id, from_user_id, from_email, message, action){
 
	//create xmlHttp object
	var xmlHttp;
	var browserName=navigator.appName; 
	
	
	if (browserName=="Netscape" || browserName=="Safari" || browserName=="Opera" || browserName=="Firefox")
	{
		xmlHttp=new XMLHttpRequest();
	}
	else if(browserName=="Microsoft Internet Explorer"){
	
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	
	}
	
	//on readystate function
	xmlHttp.onreadystatechange=function(){
	
			if(xmlHttp.readyState==4){
							
		
					if(xmlHttp.responseText!=""){
								
							//alert(xmlHttp.responseText);				
							//document.getElementById(field_id).value=xmlHttp.responseText;
							
							
							//close message pane
							if(action=="reply"){		
							
								show_msg_pane('send_msg', '0');
								
								//hide loading image since the message has been sent
								document.getElementById('msg_pane_load').style.visibility="hidden";		
								
								//call the function to list the messages 
								//list_msgs(from_user_id);
								
							}		
							//confirm that the message has been sent
							else if(action=="send"){
							
								//show the response form server
								//alert(xmlHttp.responseText);
                                   
                                p_show_msg(xmlHttp.responseText)
								
							}							
						
					}
					
					//call the function to list the messages if there has been deleted
					if(action=="delete"){
											
						list_msgs(to_user_id);
						
					}					
										
			}
	}
	
	//make sure the message field isn't empty
	if( (message=="") && (action!="delete") ){
        alert("Please type your message before sending");
    }
    else{
        switch(action){
            case "reply":
                if (cleared){
                    xmlHttp.open("POST","../manage_msg.php",true);
                    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");       
                    xmlHttp.send("&msg_id="+msg_id+"&to_user_id="+to_user_id+"&from_user_id="+from_user_id+"&from_email="+from_email+"&message="+message+"&action="+action);
                
                    //show loading image while the message is being sent
                    document.getElementById('msg_pane_load').style.visibility="visible";   
                }
                break;
            case "send":
                xmlHttp.open("POST","../manage_msg.php",true);
                xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");       
                xmlHttp.send("&msg_id="+msg_id+"&to_user_id="+to_user_id+"&from_user_id="+from_user_id+"&from_email="+from_email+"&message="+message+"&action="+action);
                p_hide_form(); 
                break;
            case "delete":
                xmlHttp.open("POST","../manage_msg.php",true);
                xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");       
                xmlHttp.send("&msg_id="+msg_id+"&to_user_id="+to_user_id+"&from_user_id="+from_user_id+"&from_email="+from_email+"&message="+message+"&action="+action);		
                break; 
        }
    }
     
	
  
}



//list testimonials
function list_testim(user_id){

	//alert("user_id: "+user_id);

	//create xmlHttp object
	var xmlHttp;
	var browserName=navigator.appName; 
	
	
	if (browserName=="Netscape" || browserName=="Safari" || browserName=="Opera" || browserName=="Firefox")
	{
		xmlHttp=new XMLHttpRequest();
	}
	else if(browserName=="Microsoft Internet Explorer"){
	
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	
	}
	
	//on readystate function
	xmlHttp.onreadystatechange=function(){
	
		if(xmlHttp.readyState==4){
	
			if(xmlHttp.responseText!="null"){
						
				//alert(xmlHttp.responseText);				
				document.getElementById('country7').innerHTML=xmlHttp.responseText;
			}
									
		}
	}
	
		
	xmlHttp.open("POST","list_testim.php",true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");       
	xmlHttp.send("&user_id="+user_id);
	
	//show loading 
	document.getElementById('country7').innerHTML="<img src='../images/loading.gif' />";	
  
}




//this functions allows the user to delete a testimonial
function manage_testim(action, user_id_to, user_id_from, from_name, message, msg_id){

	//create xmlHttp object
	var xmlHttp;
	var browserName=navigator.appName; 
	
	
	if (browserName=="Netscape" || browserName=="Safari" || browserName=="Opera" || browserName=="Firefox")
	{
		xmlHttp=new XMLHttpRequest();
	}
	else if(browserName=="Microsoft Internet Explorer"){
	
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	
	}
	
	//on readystate function
	xmlHttp.onreadystatechange=function(){
	
			if(xmlHttp.readyState==4){
		
					if(xmlHttp.responseText!=""){
								
							//alert(xmlHttp.responseText);				
							//document.getElementById(field_id).value=xmlHttp.responseText;
							
							//hide loading image while the testimonial is being sent
							//document.getElementById('msg_pane_load').style.visibility="hidden";
							
							//close message pane
							if(action=="send"){				
								//show_msg_pane('send_testim', '0');	
								
								//refresh the page after sending a testimonial
								location.href="ext_profile.php?user_id="+user_id_to+"&page=5";
			
							
							}
							else{
								
								//call the function to list the testimonials again
								list_testim(user_id_to);
							
							}
									
					}
										
			}
	}
	
	
	//make sure the testimonial field isn't empty
	if(message!="" && action=="send"){
		
			xmlHttp.open("POST","../manage_testim.php",true);
			xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");       
			xmlHttp.send("&action="+action+"&user_id_to="+user_id_to+"&user_id_from="+user_id_from+"&from_name="+from_name+"&message="+message+"&msg_id="+msg_id);
		
			//show loading image while the message is being sent
			//document.getElementById('msg_pane_load').style.visibility="visible";
			
			//alert(action);
	}
	else if(action=="delete"){
		
			xmlHttp.open("POST","../manage_testim.php",true);
			xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");       
			xmlHttp.send("&action="+action+"&user_id_to="+user_id_to+"&user_id_from="+user_id_from+"&from_name="+from_name+"&message="+message+"&msg_id="+msg_id);
		
			//show loading image while the message is being sent
			//document.getElementById('msg_pane_load').style.visibility="visible";
	}
	else{
			
			alert("Please type your message before sending");
	}	
	
  
}



//approves, requests or deletes a contact
function p_manage_contact(user_id1, user_id2, message, action){
	
	//alert("&user_id1="+user_id1+"&user_id2="+user_id2+"&message="+message+"&action="+action);
	

	//create xmlHttp object
	var xmlHttp;
	var browserName=navigator.appName; 
	
	
	if (browserName=="Netscape" || browserName=="Safari" || browserName=="Opera" || browserName=="Firefox")
	{
		xmlHttp=new XMLHttpRequest();
	}
	else if(browserName=="Microsoft Internet Explorer"){
	
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	
	}
	
	//on readystate function
	xmlHttp.onreadystatechange=function(){
	
			if(xmlHttp.readyState==4){
		
								
					//respond according to action		
					switch(action){
						
						case "list":
						  document.getElementById('country5').innerHTML=xmlHttp.responseText;
						  break;    
	
						case "request":
						  // alert(xmlHttp.responseText);
                          p_show_contact_msg(xmlHttp.responseText);
						  break;
						  
						case "approve":
						  //alert(xmlHttp.responseText);
						  
						  //list contacts again
						  manage_contact(user_id2, "", "", "list");
						  break;	
						  
						case "delete":
						  //alert(xmlHttp.responseText);
						  
						  //list contacts again
						  manage_contact(user_id1, user_id2, "", "list");
						  break;							  															
					}										
			}
	}
	
	
	//a user cannot add himself to his own contacts - doesn't make sense
	if(user_id1!=user_id2){		
			xmlHttp.open("POST","../manage_contact.php",true);
			xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");       
			xmlHttp.send("&user_id1="+user_id1+"&user_id2="+user_id2+"&message="+message+"&action="+action);
           p_hide_add_contact();
	}
	else alert("You cannot add yourself to your own contacts");
	
	
	//respond according to action		
	switch(action){
		
		case "list":
		  //show loading 
		  document.getElementById('country5').innerHTML="<img src='../images/loading.gif' />";	
		  break;    

	}	
	
	
}


//delete a user
function delete_user(user_id){

	//alert("user_id: "+user_id);

	//create xmlHttp object
	var xmlHttp;
	var browserName=navigator.appName; 
	
	
	if (browserName=="Netscape" || browserName=="Safari" || browserName=="Opera" || browserName=="Firefox")
	{
		xmlHttp=new XMLHttpRequest();
	}
	else if(browserName=="Microsoft Internet Explorer"){
	
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	
	}
	
	//on readystate function	
	xmlHttp.onreadystatechange=function(){
	
		if(xmlHttp.readyState==4){
	
						
			//alert(xmlHttp.responseText);				
			document.getElementById('del_prog').innerHTML=xmlHttp.responseText;
		
			//enable DELETE button
			document.getElementById('btn_delete').disabled=false;
									
		}
	}
	
	
	//confirm deletion of the user
	if(confirm("Are you sure you want to delete this user?")){

			xmlHttp.open("POST","delete_user.php",true);
			xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");       
			xmlHttp.send("&user_id="+user_id);
			
			//show loading 
			document.getElementById('del_prog').innerHTML="<img src='../images/loading.gif' />";	
		
			//disable DELETE button
			document.getElementById('btn_delete').disabled=true;
			
	}
  
}



//add credits to a user
function add_credits_to_user(user_id, credits){

	//alert("user_id: "+user_id+", credits: "+credits);

	//create xmlHttp object
	var xmlHttp;
	var browserName=navigator.appName; 
	
	
	if (browserName=="Netscape" || browserName=="Safari" || browserName=="Opera" || browserName=="Firefox")
	{
		xmlHttp=new XMLHttpRequest();
	}
	else if(browserName=="Microsoft Internet Explorer"){
	
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	
	}
	
	//on readystate function	
	xmlHttp.onreadystatechange=function(){
	
		if(xmlHttp.readyState==4){
	
						
			//alert(xmlHttp.responseText);				
			document.getElementById('add_prog').innerHTML=xmlHttp.responseText;
		
			//enable DELETE button
			document.getElementById('btn_add_cred').disabled=false;
									
		}
	}
	
	
	//confirm deletion of the user
	if(confirm("Please confirm: add "+credits+" credits to user_id:"+user_id)){

			xmlHttp.open("POST","add_credits_to_user.php",true);
			xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");       
			xmlHttp.send("&user_id="+user_id+'&credits='+credits);
			
			//show loading 
			document.getElementById('add_prog').innerHTML="<img src='../images/loading.gif' />";	
		
			//disable DELETE button
			document.getElementById('btn_add_cred').disabled=true;
			
	}
  
}



//list news articles
function list_news(tag_id, admin){

	//create xmlHttp object
	var xmlHttp;
	var browserName=navigator.appName; 
	
	
	if (browserName=="Netscape" || browserName=="Safari" || browserName=="Opera" || browserName=="Firefox")
	{
		xmlHttp=new XMLHttpRequest();
	}
	else if(browserName=="Microsoft Internet Explorer"){
	
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	
	}
	
	//on readystate function
	xmlHttp.onreadystatechange=function(){
	
		if(xmlHttp.readyState==4){
	
			//alert(xmlHttp.responseText);				
			document.getElementById(tag_id).innerHTML=xmlHttp.responseText;
									
		}
	}
	
		
	xmlHttp.open("POST","list_news.php",true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");       
	xmlHttp.send("&admin="+admin);
	
	//alert(admin);
	
	//show loading 
	document.getElementById(tag_id).innerHTML="<img src='../images/loading.gif' />";	
  
}




//this functions allows the user to delete a testimonial
function manage_news(action, title, the_news, news_id, tag_id, admin){	

		//create xmlHttp object
		var xmlHttp;
		var browserName=navigator.appName; 
		
		
		if (browserName=="Netscape" || browserName=="Safari" || browserName=="Opera" || browserName=="Firefox")
		{
			xmlHttp=new XMLHttpRequest();
		}
		else if(browserName=="Microsoft Internet Explorer"){
		
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		
		}
		
		//on readystate function
		xmlHttp.onreadystatechange=function(){
		
				if(xmlHttp.readyState==4){		
									
								
					//call the function to list news articls again
					list_news(tag_id, admin);				
					
					//alert(xmlHttp.responseText);
											
				}
		}
		
		
		//make sure the news article field isn't empty
		if(the_news=="" && action=="submit"){
			
				alert("Please type your text before posting the news");
			
		}
		else{
				
				xmlHttp.open("POST","manage_news.php",true);
				xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");       
				xmlHttp.send("&action="+action+"&title="+title+"&the_news="+the_news+"&news_id="+news_id);								
				
		}	
	
  
}


//list supscriptions to spotlight for admin approval
function list_supscrip(tag_id){

	//create xmlHttp object
	var xmlHttp;
	var browserName=navigator.appName; 
	
	
	if (browserName=="Netscape" || browserName=="Safari" || browserName=="Opera" || browserName=="Firefox")
	{
		xmlHttp=new XMLHttpRequest();
	}
	else if(browserName=="Microsoft Internet Explorer"){
	
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	
	}
	
	//on readystate function
	xmlHttp.onreadystatechange=function(){
	
		if(xmlHttp.readyState==4){
	
			//alert(xmlHttp.responseText);				
			document.getElementById(tag_id).innerHTML=xmlHttp.responseText;
									
		}
	}
	
		
	xmlHttp.open("POST","list_supscrip.php",true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");       
	xmlHttp.send();
	
	//alert(admin);
	
	//show loading 
	document.getElementById(tag_id).innerHTML="<img src='../images/loading.gif' />";	
  
}


//supscriptions for spotlights,
//paid VIP membership of company profile activations/updat
function manage_supscr(user_id, type, date_start, num_weeks, my_action, price, is_admin){
	
	if(!is_admin)
		var folder_prefix="../";
	else if (is_admin)
		var folder_prefix="";
	
	//create xmlHttp object
	var xmlHttp;
	var browserName=navigator.appName; 
	
	
	if (browserName=="Netscape" || browserName=="Safari" || browserName=="Opera" || browserName=="Firefox")
	{
		xmlHttp=new XMLHttpRequest();
	}
	else if(browserName=="Microsoft Internet Explorer"){
	
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	
	}
	
	//on readystate function
	xmlHttp.onreadystatechange=function(){
	
		if(xmlHttp.readyState==4){
	
			//document.getElementById('gal_list').innerHTML=xmlHttp.responseText;
			//alert(xmlHttp.responseText);
			
			
			if(my_action=='subscr'){
					//refresh the credits balance
					get_credits_balance(user_id);
					
					//refresh supscriptions list
					supscr_summary(user_id, "supscrip_list");
			}
			else if(my_action=='delete' || my_action=='approve'){
				
					list_supscrip("appr_spot");
				
			}
									
		}
	}
	
	//if the user wants to request a supscription
	if(my_action=="subscr"){
	
			if(document.getElementById("next_date").value!="" && document.getElementById("spotl_price").value!="" || type>=4){
		
			
				//make sure the user has enought credits
				if( verf_balance(price, num_weeks, document.getElementById("balance").value)==0){
						
					//portfolio professionnel
					if(type == 4 && confirm("Cette inscription vous soustraira "+num_weeks*price+ " credits, veullez confirmer \n\nThis supscription will cost you "+num_weeks*price+ " credits, please confirm")){
						xmlHttp.open("POST",folder_prefix+"manage_supscr.php",true);
						xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");       
						xmlHttp.send("&user_id="+user_id+"&type="+type+"&date_start="+date_start+"&num_weeks="+num_weeks+"&my_action="+my_action);
					}
					//spotlight
					else if(type != 4 && confirm("Cette inscription vous soustraira "+num_weeks*price+ " credits, veullez confirmer \n\nThis supscription will cost you "+num_weeks*price+ " credits, please confirm")){
						xmlHttp.open("POST",folder_prefix+"manage_supscr.php",true);
						xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");       
						xmlHttp.send("&user_id="+user_id+"&type="+type+"&date_start="+date_start+"&num_weeks="+num_weeks+"&my_action="+my_action);
					}
				
				}
				else{
				
					alert("Il vous manque "+verf_balance(price, num_weeks, document.getElementById("balance").value)+" credits");
					
					//write in the nuber of missing credits to buy
					document.getElementById('quantity').value=verf_balance(price, num_weeks, document.getElementById("balance").value);
					
					//calculate total for the purchaise for credits
					calc_amount();
				}
					
			}
			else alert("Veuillez selectionner le type de parrution");
			
	}
	//if the admin wants to approve or delete/decline the request
	else if(my_action=="delete" || my_action=="approve"){					
		
			xmlHttp.open("POST",folder_prefix+"manage_supscr.php",true);
			xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");       
			xmlHttp.send("&user_id="+user_id+"&type="+type+"&date_start="+date_start+"&num_weeks="+num_weeks+"&my_action="+my_action);	
			
			//alert("&user_id="+user_id+"&type="+type+"&date_start="+date_start+"&num_weeks="+num_weeks+"&my_action="+my_action);
	}
  
}



//sets newsletter preferenc
function toggle_newsletter(user_id, checkbox, tag_id){
	
	
	//create xmlHttp object
	var xmlHttp;
	var browserName=navigator.appName; 
	
	
	if (browserName=="Netscape" || browserName=="Safari" || browserName=="Opera" || browserName=="Firefox")
	{
		xmlHttp=new XMLHttpRequest();
	}
	else if(browserName=="Microsoft Internet Explorer"){
	
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	
	}
	
	
	//on readystate function
	xmlHttp.onreadystatechange=function(){
	
		if(xmlHttp.readyState==4){
	
			//alert(xmlHttp.responseText);				
			document.getElementById(tag_id).innerHTML=xmlHttp.responseText;
									
		}
	}
	
	var action;
	
	if(checkbox.checked)
		action="1";
		
	else action="0";	
	
		
	xmlHttp.open("POST","../manage_newslet.php",true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");       
	xmlHttp.send("&user_id="+user_id+"&action="+action);	
	
	
}



//check the format of the item to be published
function check_item_format(form_id){
	
	//enable file uploader
	document.getElementById(form_id).uploadedfile.disabled=false;
	
	
	//alert(form_id);
	
	//if not a youtube video
	if(document.getElementById(form_id).type.value!="4"){

			var file_name=document.getElementById(form_id).uploadedfile;
			
			
			if(file_name.value==""){
			
				document.getElementById('err_msg5').innerHTML="&nbsp;Please choose a file<img src='../images/small_strike.gif' width='18' height='18' align='absmiddle' style='margin-left:10px;'/>";
				return false;
			
			}
			//make sure correct file format is chosen
			else if(   file_name.value.substring(file_name.value.length-3,file_name.value.length).toLowerCase()=="pdf" ||
					   file_name.value.substring(file_name.value.length-3,file_name.value.length).toLowerCase()=="doc" ||
					   file_name.value.substring(file_name.value.length-4,file_name.value.length).toLowerCase()=="docx" ||
					   file_name.value.substring(file_name.value.length-3,file_name.value.length).toLowerCase()=="mp3"){
					
					return true;
			}	
			else return false;
			
	}
	//if its a yotube video
	else{
			
			document.getElementById(form_id).uploadedfile.disabled="disabled";
			return true;
	}
	
}


//simply submits a form without any other actions
function submit_form(form_id){	

	document.getElementById(form_id).submit();
	
	//alert("submit_form");

}



//submit's a form with an item to be published
function submit_item_form(form_id){	


	var form_ok=false;
	

	//check file type
	if(check_item_format(form_id)){	
			
			document.getElementById('err_msg5').innerHTML="<img src='../images/checkmark.gif' width='18' height='18' align='absmiddle' style='margin-left:10px;'/>";									
			
			form_ok=true;
			
	}
	else {
		
		document.getElementById('err_msg5').innerHTML="<img src='../images/small_strike.gif' width='18' height='18' align='absmiddle' style='margin-left:10px;'/>&nbsp;Wrong file format";	
		
		form_ok=false;	
		
	}	
	
	
	
	//check the title
	if(document.getElementById(form_id).title.value!=""){
		
		document.getElementById('err_msg4').innerHTML="<img src='../images/checkmark.gif' width='18' height='18' align='absmiddle' style='margin-left:10px;'/>";			
		
	}
	else {
		
		document.getElementById('err_msg4').innerHTML="<img src='../images/small_strike.gif' width='18' height='18' align='absmiddle' style='margin-left:10px;'/>&nbsp;Please type a title";	
		
		form_ok=false;	
	}
	
	
	
	//check the type
	if(document.getElementById(form_id).type.value!="0"){
		
		document.getElementById('err_msg6').innerHTML="<img src='../images/checkmark.gif' width='18' height='18' align='absmiddle' style='margin-left:10px;'/>";			
		
	}
	else {
		
		document.getElementById('err_msg6').innerHTML="<img src='../images/small_strike.gif' width='18' height='18' align='absmiddle' style='margin-left:10px;'/>&nbsp;Please select a type";	
		
		form_ok=false;	
	}
	
	
	//submit form
	if(form_ok){
	
		//assign upload id to the item to avoid duplicates
		document.getElementById(form_id).item_upload_id.value=get_upload_id(15);	
		
		document.getElementById(form_id).submit();
	}
	
}	



//generate upload id
// Define local variables.
function get_upload_id(str_length){

	var intI = 0;
	var strLargeText = "";
	var intValue = 0;
	 
	
	// Loop over number of characters in string.
	for (intI = 0 ; intI < str_length; intI++){
	 
	
	// Get a random value between 33 and 126. We are going
	// to use these to pick characters out of the ascii
	// table.
	intValue = RandRange(97, 122, intI );
	 
	
	// Append a character that is randomly chosen
	strLargeText += String.fromCharCode( intValue );
	 
	
	}
	 
	
	// Alert the ramdomly created string.
	return strLargeText;
	
}

function RandRange( intFrom, intTo, intSeed ){
	// Make sure that we have integers.
	intFrom = Math.floor( intFrom );
	intTo = Math.floor( intTo );
	 
	
	// Return the random number.
	return(
	Math.floor(
	intFrom + 
	(
	(intTo - intFrom + 1) * 
	 
	
	// Seed the random number if a value was passed.
	Math.random( 
	(intSeed != null) ? intSeed : 0 
	)
	))
	);
}


//manage refferals
function manage_refferals(action, user_id, to_name, to_email, message, ref_code, form_id){

	//alert(action+", "+user_id+", "+to_name+", "+to_email+", "+message+", "+ref_code);

	//create xmlHttp object
	var xmlHttp;
	var browserName=navigator.appName; 
	
	
	if (browserName=="Netscape" || browserName=="Safari" || browserName=="Opera" || browserName=="Firefox")
	{
		xmlHttp=new XMLHttpRequest();
	}
	else if(browserName=="Microsoft Internet Explorer"){
	
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	
	}
	
	//on readystate function
	xmlHttp.onreadystatechange=function(){
	
		if(xmlHttp.readyState==4){
										
			
			//hide loading
			document.getElementById('send_ref_load').innerHTML="";
			
			//enable send button again
			document.getElementById("btn_send_ref").disabled=false;
			
			//clear the fields
			document.getElementById('to_name').value="";
			document.getElementById('to_email').value="";
			document.getElementById('message').value="";
			
			//confirm 
			alert(xmlHttp.responseText);

									
		}
	}
	
	
	if(to_name!="" && to_email!="" && message!=""){
		
			xmlHttp.open("POST","../manage_refferals.php",true);
			xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");       
			xmlHttp.send("&action="+action+"&user_id="+user_id+"&to_name="+to_name+"&to_email="+to_email+"&message="+message+"&ref_code="+ref_code);
			
			
			//show loading 
			document.getElementById('send_ref_load').innerHTML="<img src='../images/loading.gif' />";	
			
			//temporary disable send button
			document.getElementById("btn_send_ref").disabled=true;	
	}
	else alert("Please fill in all the fields");
  
}



//list paid billboard postings for admin approval
function list_billboard(tag_id, admin){

	//create xmlHttp object
	var xmlHttp;
	var browserName=navigator.appName; 
	
	
	if (browserName=="Netscape" || browserName=="Safari" || browserName=="Opera" || browserName=="Firefox")
	{
		xmlHttp=new XMLHttpRequest();
	}
	else if(browserName=="Microsoft Internet Explorer"){
	
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	
	}
	
	//on readystate function
	xmlHttp.onreadystatechange=function(){
	
		if(xmlHttp.readyState==4){
	
			//alert(xmlHttp.responseText);				
			document.getElementById(tag_id).innerHTML=xmlHttp.responseText;
									
		}
	}
	
		
	xmlHttp.open("POST","list_billboard.php",true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");       
	xmlHttp.send("&admin="+admin);
	
	//alert(admin);
	
	//show loading 
	document.getElementById(tag_id).innerHTML="<img src='../images/loading.gif' />";	
  
}


//delete/approve paid billboard postings for admin approval
function manage_billboard(tag_id, ad_id, action){

	//create xmlHttp object
	var xmlHttp;
	var browserName=navigator.appName; 
	
	
	if (browserName=="Netscape" || browserName=="Safari" || browserName=="Opera" || browserName=="Firefox")
	{
		xmlHttp=new XMLHttpRequest();
	}
	else if(browserName=="Microsoft Internet Explorer"){
	
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	
	}
	
	//on readystate function
	xmlHttp.onreadystatechange=function(){
	
		if(xmlHttp.readyState==4){
	
			//alert(xmlHttp.responseText);				
			document.getElementById(tag_id).innerHTML=xmlHttp.responseText;
			
			//show billboard postings
			list_billboard(tag_id, 1);
									
		}
	}
	
		
	xmlHttp.open("POST","manage_billb.php",true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");       
	xmlHttp.send("&ad_id="+ad_id+"&action="+action);
	
	
	//show loading 
	document.getElementById(tag_id).innerHTML="<img src='../images/loading.gif' />";	
  
}


//*********** mouse coord. related functions for FireFox
var mouseX, mouseY;

function getMousePos(e)
{
	if (!e)
		
		var e = window.event||window.Event;
	
	if('undefined'!=typeof e.pageX){

		mouseX = e.pageX;
		mouseY = e.pageY;
	}
	else{
		
		mouseX = e.clientX + document.body.scrollLeft;
		mouseY = e.clientY + document.body.scrollTop;
	}

}

// You need to tell Mozilla to start listening:

if(window.Event && document.captureEvents)

	document.captureEvents(Event.MOUSEMOVE);

// Then assign the mouse handler

document.onmousemove = getMousePos;
//*************************************

//shows a zoom of the image on mouseover
function zoom(user_id, photo_id, lang){	


//alert(document.body.scrollTop);
	
	
//create xmlHttp object
var xmlHttp;
var browserName=navigator.appName; 


if (browserName=="Netscape" || browserName=="Safari" || browserName=="Opera" || browserName=="Firefox")
{
	xmlHttp=new XMLHttpRequest();
}
else if(browserName=="Microsoft Internet Explorer"){

	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

}
	
	

	
	if(document.getElementById("zoom_photo").style.visibility=="hidden"){
				
		
		document.getElementById("zoom_photo").style.visibility="visible";
	
		
		if (browserName=="Netscape" || browserName=="Firefox")
		{
			
				document.getElementById("zoom_photo").style.left=mouseX+15+"px";
				document.getElementById("zoom_photo").style.top=mouseY+15+window.pageYOffset+"px";	
			
		}
		else{
		
				document.getElementById("zoom_photo").style.left=event.clientX+15+"px";
				document.getElementById("zoom_photo").style.top=1000+"px"; //event.clientY+15+1.2*parseInt(document.documentElement.scrollTop);	
				
				//document.title=document.getElementById("zoom_photo").style.top=event.clientY+15+document.documentElement.scrollTop+"px";
		
		}
		
	
		
		//document.getElementById("zoom_photo").innerHTML="_"+user_id+"_"+photo_id;
			
		
		//on readystate function
		xmlHttp.onreadystatechange=function(){
		
			if(xmlHttp.readyState==4){
		
				//alert(xmlHttp.responseText);				
				document.getElementById("zoom_photo").innerHTML=xmlHttp.responseText;				
										
			}
		}
				
			
		xmlHttp.open("POST","prev_prof.php",true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");       
		xmlHttp.send("&user_id="+user_id+"&photo_id="+photo_id+"&lang="+lang);
		
		
		//show loading 
		document.getElementById("zoom_photo").innerHTML="<img src='../images/loading.gif' />";	
		

	}
	else if(document.getElementById("zoom_photo").style.visibility=="visible"){
		
		document.getElementById("zoom_photo").style.visibility="hidden";
		
	}
	
}

//moves the zoom div along with mouse
function snap_div_to_mouse(){
	
		
		var browserName=navigator.appName;


		if (browserName=="Netscape" || browserName=="Firefox")
		{
			
				document.getElementById("zoom_photo").style.left=mouseX+15+"px";
				document.getElementById("zoom_photo").style.top=mouseY+15+"px";
			
		}
		else{
		
				document.getElementById("zoom_photo").style.left=event.clientX+15+"px";
				document.getElementById("zoom_photo").style.top=event.clientY+15+document.documentElement.scrollTop+"px";
		
		}


}


//changes the label of the field for public items
function swap_label(label_id, upload_file, index){
	
	var label=Array("Description:", "Description of your PDF document:", "Description of your Word document:", "About your MP3 file:", "Paste your video object here:", "Description of your resume");
	
	
	document.getElementById(label_id).innerHTML=label[index];	
	
	if(index==4)
		document.getElementById(upload_file).disabled=true;

	else document.getElementById(upload_file).disabled=false;

}



//choose template
function set_template(user_id, template){	


	set_backgr(template);



	//create xmlHttp object
	var xmlHttp;
	var browserName=navigator.appName; 
	
	
	if (browserName=="Netscape" || browserName=="Safari" || browserName=="Opera" || browserName=="Firefox")
	{
		xmlHttp=new XMLHttpRequest();
	}
	else if(browserName=="Microsoft Internet Explorer"){
	
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	
	}
	
	//on readystate function
	xmlHttp.onreadystatechange=function(){
	
		if(xmlHttp.readyState==4){
	
			//alert(xmlHttp.responseText);				
			//document.getElementById(tag_id).innerHTML=xmlHttp.responseText;
									
		}
	}
	
		
	xmlHttp.open("POST","../set_template.php",true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");       
	xmlHttp.send("&user_id="+user_id+"&template="+template);
	
	
	//show loading 
	//document.getElementById(tag_id).innerHTML="<img src='../images/loading.gif' />";	


}


//change background of the template chooser
function set_backgr(template){
	
	if(template.substring(0,1)=='1')
		document.getElementById("theme1").style.backgroundImage="url(../images/templ_thumb_"+template+".jpg)";
		
	else if(template.substring(0,1)=='2')
		document.getElementById("theme2").style.backgroundImage="url(../images/templ_thumb_"+template+".jpg)";
		
	else if(template.substring(0,1)=='3')
		document.getElementById("theme3").style.backgroundImage="url(../images/templ_thumb_"+template+".jpg)";
		
	else if(template.substring(0,1)=='4')
		document.getElementById("theme4").style.backgroundImage="url(../images/templ_thumb_"+template+".jpg)";
		
	else if(template.substring(0,1)=='5')
		document.getElementById("theme5").style.backgroundImage="url(../images/templ_thumb_"+template+".jpg)";
	//alert("ok");
}

//check the checkbox corresponding to the choosen template
function templ_checkbox(template, form_id){

	var checkboxes=new Array();
	
	checkboxes=document.getElementById(form_id).elements;

	//alert(checkboxes.length);
	
	//check the checkbox corresponding to the choosen tempalte
	for(i=0; i<checkboxes.length; i++){
	
		if(checkboxes[i].type=="radio" && checkboxes[i].value==template){
	
			checkboxes[i].checked=true;		
			
			//alert(checkboxes[i].value);
			
		}
		
	}
}


//sends email form wensite's main contact page
function SendInq(from, from_email, subject, message){

	//create xmlHttp object
	var xmlHttp;
	var browserName=navigator.appName; 
	
	//alert(browserName);
	
	if (browserName=="Netscape" || browserName=="Safari" || browserName=="Opera" || browserName=="Firefox")
	{
		xmlHttp=new XMLHttpRequest();
	}
	else if(browserName=="Microsoft Internet Explorer"){
	
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	
	}
	
	//on readystate function
	xmlHttp.onreadystatechange=function(){
	
		if(xmlHttp.readyState==4){
	
			//receive confirmation from server
			document.getElementById('err_msg4').innerHTML=xmlHttp.responseText;
			
			//clear fields' values															
			document.form_mail.from_email.value="";
			document.form_mail.from.value="";	
			document.form_mail.subject.value="";
			document.form_mail.message.value="";
			
			//enable the fields again
			document.form_mail.disabled=false;					
			
			//hide loading image
			document.getElementById('loading_img').style.visibility="hidden";

		}
	}
	
	//check than all fields are complete
	if(from=="" || from_email=="" || message=="" || subject==""){
	
		document.getElementById('err_msg4').innerHTML="Please complete all the fields <br />Veuillez compl&eacute;ter tous les champs";
	
	}
	else {	
			//disable all the fields			
			document.form_mail.disabled=true;
			
			//show the loading image
			document.getElementById('loading_img').style.visibility="visible";						
				
			//send the inquiry
			xmlHttp.open("POST","send_inq.php",true);
			xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");       
			xmlHttp.send("&from="+from+"&from_email="+from_email+"&subject="+subject+"&message="+escape(message));
			
	}

  
}



//set main photo, main photo will appear in search engine, spotlight and on the first page of user profile
function set_main_photo(radio_button, user_id, photo_id){
	
	//uncheck all other radio buttons
	for(i=1;i<=10;i++){
	
		
		if(document.getElementById('radio_gr_ph_'+i) && document.getElementById('radio_gr_ph_'+i).id!=radio_button.id){
						
			document.getElementById('radio_gr_ph_'+i).checked=false;
		
		}
	
	}
	
	//create xmlHttp object
	var xmlHttp;
	var browserName=navigator.appName; 
	
	
	if (browserName=="Netscape" || browserName=="Safari" || browserName=="Opera" || browserName=="Firefox")
	{
		xmlHttp=new XMLHttpRequest();
	}
	else if(browserName=="Microsoft Internet Explorer"){
	
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	
	}
	
	//on readystate function
	xmlHttp.onreadystatechange=function(){
	
		if(xmlHttp.readyState==4){
	
			//alert(xmlHttp.responseText);				
			//document.getElementById(tag_id).innerHTML=xmlHttp.responseText;
									
		}
	}
	
		
	xmlHttp.open("POST","../set_main_photo.php",true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");       
	xmlHttp.send("&user_id="+user_id+"&photo_id="+photo_id);
	
	
	//show loading 
	//document.getElementById(tag_id).innerHTML="<img src='../images/loading.gif' />";	

}



//limits the number of characters entered into a textarea or another input
function limit_textarea_by_char(input_id, count_id, limit){	
		
	
	//check if limit is reached
	if(document.getElementById(input_id).value.length >= limit){
	
		//remove last char
		document.getElementById(input_id).value=document.getElementById(input_id).value.substr(0,850);
		
		//document.getElementById(count_id).innerHTML=limit-document.getElementById(input_id).value.length;
	
	}
	//show number of characters entered so far
	else{
		
		//document.getElementById(count_id).innerHTML=limit-document.getElementById(input_id).value.length;
	
	}
	
}


//checks if accented characters have been entered
function ValidateUsername(obj)
{
	var sUsername = obj == null ? "" : obj.value;

	if(sUsername.match(/[^\w]|[A-Z]/)==null) {		
	
		return true;
	}

	return false;
}
























