/**

 * Custom Javascript 

 *

 * @author Wen Cano

 */



/* Toggle Display by ID 

 * 

 * Example: <a href="javascript:;" onclick="toggledisplay(['elementid1', 'elementid2']);">Toggle</a>

 */
function toggledisplay(ids) {
	for(i=0;i<ids.length;i++) {
		var element = document.getElementById(ids[i]);		
		//Clear output div value if not already cleared
		if (ids.length>2 && i==ids.length-1){
			if (element.value!=''){
			//alert(element.value);
			element.value='';
			}
		}
		if (element.style.display=='' || element.style.display=='block') 
			element.style.display = 'none';
		else 
			element.style.display = 'block';
	}
}


function showForm(ids) {
	for(i=0;i<ids.length;i++) {
		var element = document.getElementById(ids[i]);		
		//Clear output div value if not already cleared
		if (ids.length>2 && i==ids.length-1){
			if (element.value!=''){
			//alert(element.value);
			element.value='';
			}
		}
		if (element.style.display=='' || element.style.display=='block') 
			element.style.display = 'none';
		else 
			element.style.display = 'block';			
			var x=document.getElementById("xchange_item_type").selectedIndex;
			var y=document.getElementById("xchange_item_type").options;
//			alert("Value: " + y[x].value + " is " + y[x].text);
			showItemForm(y[x].value);
	}
}


//Functions added by Shashi Prakash
function displaybuy(ids){
	for (i=0;i<ids.length;i++){
		var numb = ids[i].match(/\d/g);
		numb = numb.join("");
		var element = document.getElementById(ids[i]);
		if (element.style.display=='' || element.style.display=='block'){
			element.style.display = 'none';
			element.value='';
		}
		else{
			element.style.display = 'block';
			display_captcha(numb);			
		}
	}
}


function display_captcha(id){
	var output_div = "buy_button"+id;
	new Ajax.Updater (output_div, base_url+'myexample_captcha/', {method:'post' , postBody: 'captcha='+''});
}

function setXchangeOptions(o, id){	 
	if(o=="1")
	{
		var element = document.getElementById(id);
		element.style.display = 'block';
	}else{
		var element = document.getElementById(id);
		element.style.display = 'none';
	}
}

function showItemForm(id){
		var all_goods = document.getElementById("all_goods");	 
		var books = document.getElementById("books");	 
		var fittness = document.getElementById("fittness");
		var tutoring = document.getElementById("tutoring");
		var travel = document.getElementById("travel");
		var houses = document.getElementById("houses");
		var housemates = document.getElementById("housemates");
		var jobs = document.getElementById("jobs");
		
		all_goods.style.display = 'none';
		books.style.display = 'none';
		fittness.style.display = 'none';
		tutoring.style.display = 'none';
		travel.style.display = 'none';
		houses.style.display = 'none';
		housemates.style.display = 'none';
		jobs.style.display = 'none';
	/* 
		1	=	Books
		2	=	Electronics
		3	=	Furniture
		4	=	Miscellanous
		5	=	Auto
		6	=	Computers
		7	=	Sporting
		8	=	School Related
		23	=	Appliences
		24	=	Clothes
		25	=	Entertainment
		
		12	=	Fittness
		13	=	Tutoring
		14	=	Travel
		
		16	=	Apartment
		9	=	Houses
		17	=	Rommates
		
		19	=	Administrative
		21	=	Internship
		22	=	Other
		20	=	Research		
	*/
	var goods = new Array(2,3,4,5,6,7,8,23,24,25);
	var aphouse = new Array(16,9);
	var alljobs = new Array(19,20,21,22);

	if(find_in_array(goods, id)){
		all_goods.style.display = 'block';
		document.all_goods.xchange_item_category.value = id;
	}else if(id == 1){
		books.style.display = 'block';
		document.books.xchange_item_category.value = id;				
	}else if(id == 12){
		fittness.style.display = 'block';
		document.fittness.xchange_item_category.value = id;				
	}else if(id == 13){
		tutoring.style.display = 'block';
		document.tutoring.xchange_item_category.value = id;				
	}else if(id == 14){
		travel.style.display = 'block';
		document.travel.xchange_item_category.value = id;					
	}else if(find_in_array(aphouse, id)){
		houses.style.display = 'block';
		document.houses.xchange_item_category.value = id;		
	}else if(id == 17){
		housemates.style.display = 'block';
		document.housemates.xchange_item_category.value = id;		
	}else if(find_in_array(alljobs, id)){
		document.jobs.xchange_item_category.value = id;
		jobs.style.display = 'block';
	}
} 

function find_in_array(arr, obj) {
  for(var i=0; i<arr.length; i++) {
    if (arr[i] == obj) return true;
  }
}

function deleteImg(image_no){
	var ans = window.confirm('Are you sure???');
	if(ans){
		document.getElementById("img"+image_no).innerHTML = '<input type="file" name="xchange_item_file'+image_no+'" class="inputtext" />';
	}
}

//AJAX functions implemented below

window.onload = function () {
	for(i=1;i<=30;i++){
		var form_offer='form_makeoffer'+i;
		$(form_offer).onsubmit = function () {
			makeoffer_results();
			return false;	
		}

		var form_message='form_messageseller'+i;
		$(form_message).onsubmit = function () {
			messageseller_results();
			return false;
		}

		var form_captcha = 'captcha_form'+i;
		$(form_captcha).onsubmit = function(){
		   check_captcha();
		   return false;
		}
	}
}



function makeoffer_results() {
	var output_div;

	var xchange_offer_id;

	for(i=1;i<=30;i++){

		xchange_offer_id='xchange_offer'+i;

		xchange_id = 'hidden_xid'+i;

		output_div = 'output'+i;

		if($F(xchange_offer_id)!=''){

			var variable = document.getElementById(xchange_id).value; 

			new Ajax.Updater (output_div, base_url+'xchange/make_offer/'+i, {method:'post', postBody: xchange_id+'='+variable+'&'+xchange_offer_id+'='+$F(xchange_offer_id)});

			new Effect.Appear(output_div);

			//Important to clear form value and output 'div' value as the 'if' 

			//condition will become true for entries that aren't meant to go to database

			document.getElementById(output_div).value=""; //Interacts with the toggledisplay() function in xchange_view and shows up incorrectly if not cleared

			document.getElementById(xchange_offer_id).value="";

		}

	}

}

function messageseller_results() {

	var output_div;

	var xchange_message_id;

	for(i=1;i<=30;i++){

		xchange_message_id='xchange_message'+i;

		xchange_id = 'hidden_xid'+i;

		output_div = 'output'+i;

		if($F(xchange_message_id)!=''){

			var variable = document.getElementById(xchange_id).value;

			new Ajax.Updater (output_div, base_url+'xchange/send_message/'+i, {method:'post', postBody: xchange_id+'='+variable+'&'+xchange_message_id+'='+$F(xchange_message_id)});

			new Effect.Appear(output_div);

			//Important to clear form value and output 'div' value as the 'if' 

			//condition will become true for entries that aren't meant to go to database

			document.getElementById(output_div).value=""; //Interacts with the toggledisplay() function in xchange_view and shows up incorrectly if not cleared

			document.getElementById(xchange_message_id).value="";

		}

	}

}

function check_captcha(){

   //alert("got in!!!");

   dont_clear = '';	

   for (i=1;i<=30;i++){

        captcha_id = 'captcha'+i;

		hidden_id = 'hidden_xid'+i;

		//alert(captcha_id);

   		if($F(captcha_id)!=''){

		   dont_clear = i;

		   output_div = 'output'+i;

		   submit_id = 'submit'+i;

		   //alert($F(captcha_id));

		   new Ajax.Updater(output_div, base_url+'myexample_captcha/', {method:'post', postBody: 'captcha='+$F(captcha_id)+'&'+'xchange_id='+$F(hidden_id)});

		   new Effect.Appear(output_div);

		   //document.getElementById(output_div).value="";

		}

   }

   for (i=1;i<=30;i++){

        if (dont_clear != i){

   		captcha_id = 'captcha'+i;

		captcha_id.value="";

		}

   }

}
