// New Profile Layout JavaScript Document - shankar
var NewProfile 		= Class.create();
NewProfile.prototype = {
	initialize : function(event, ident, userident){
		switch(event){
			case "EditBasicInfo":
				this.EditBasicInfoJS();
				break;
			case "CancelBasicInfo":
				this.CancelBasicInfoJS();
				break;
			case "SaveBasicInfo":
				this.SaveBasicInfoJS();
				break;
			case "EditLinksInfo":
				this.EditLinksInfoJS();
				break;
			case "SaveLinskInfo":
				this.SaveLinskInfoJS();
				break;
			case "CancelLinskInfo":
				this.CancelLinskInfoJS();
				break;				
			case "EditArtistInfo":
				this.EditArtistInfoJS();
				break;
			case "SaveArtistInfo":
				this.SaveArtistInfoJS();
				break;
			case "CancelArtistInfo":
				this.CancelArtistInfoJS();
				break;
			case "EditTasteInfo":
				this.EditTasteInfoJS();
				break;
			case "SaveTasteInfo":
				if(this.CheckDuplicateMusicJS()=="")
					this.SaveTasteInfoJS();
				else
					alert("You have selected "+this.CheckDuplicateMusicJS()+" more than once.");
				break;
			case "CancelTasteInfo":
				this.CancelTasteInfoJS();
				break;
			case "ClearLinksErrorDivs":
				this.ClearLinksErrorDivsJS();
				break;
			case "ClearBInfoErrorDivs":
				this.ClearBInfoErrorDivsJS();
				break;
			case "CheckDuplicateMusic":
				this.CheckDuplicateMusicJS();
				break;
			case "CheckMusicUnique":
				this.CheckMusicUniqueJS(ident);
				break;
				
		}
	},
	EditBasicInfoJS:function()
	{
		/* HIDE ALL DETAILS LABEL AND SHOW TEXTBOX */		
		$('detail_location_info').style.display 	= "none";
		$('detail_country_info').style.display 		= "none";
		$('detail_year_select').style.display 		= "none";
		$('detail_dob_info').style.display 			= "none";
		$('detail_dob_info').style.display 			= "none";
		$('details_btn_info').style.display 		= "none";
		
		/* TEXT BOXES DISPLAY */		
		$('detail_state_txtbox').style.display 		= "";
		$('detail_city_txtbox').style.display 		= "";
		$('detail_country_txtbox').style.display 	= "";
		$('detail_year_select').style.display 		= "";
		$('detail_month_select').style.display 		= "";
		$('detail_day_select').style.display 		= "";
		$('details_btn_save').style.display 		= "";
		$('details_btn_cancel').style.display 		= "";
	},
	CancelBasicInfoJS:function()
	{
		/* TEXT BOXES DISPLAY */		
		$('detail_state_txtbox').style.display 		= "none";
		$('detail_city_txtbox').style.display 		= "none";
		$('detail_country_txtbox').style.display	= "none";
		$('detail_year_select').style.display 		= "none";
		$('detail_month_select').style.display 		= "none";
		$('detail_day_select').style.display 		= "none";
		$('details_btn_save').style.display 		= "none";
		$('details_btn_cancel').style.display 		= "none";
		
		/* HIDE ALL DETAILS LABEL AND SHOW TEXTBOX */		
		$('detail_location_info').style.display 	= "";
		$('detail_country_info').style.display 		= "";
		$('detail_dob_info').style.display 			= "";
		$('details_btn_info').style.display 		= "";
		
		/* AJAX REQ */	
		var userident	= $('UserIdent').value;
		var url 		= SiteAjaxPath+"profile_ajax.php";
		var pars		= "objAction=CancelInfo&user_ident="+userident;
		var myAjax 		= new Ajax.Request(url, {method:'post',postBody:pars, onSuccess:this.CancelBasicInfoCompleteJS, onFailure:this.showcontactsFailed});

	},
	CancelBasicInfoCompleteJS:function(t)
	{
		var myJSONObject 	= parseJSON(t.responseText);
		var BasicInfo 		= null;
		BasicInfo 	 		= myJSONObject.UserDetails;
		
		$('UserState').value 	= BasicInfo.state;
		$('UserCity').value 	= BasicInfo.city;
		$('country').value		= BasicInfo.country;
		$('UserMonth').value	= BasicInfo.birthmonth;
		$('UserYear').value		= BasicInfo.birthyear;
		$('UserDay').value		= BasicInfo.birthday;
		
		$('detail_location_info').innerHTML = BasicInfo.city+" , "+BasicInfo.state;
		$('detail_country_info').innerHTML 	= BasicInfo.CountryName;
		$('detail_dob_info').innerHTML 		= BasicInfo.Age;
		
		new NewProfile('ClearBInfoErrorDivs');
	},
	SaveBasicInfoJS:function()
	{
		var userstate 		= $('UserState').value;
		var usercity		= $('UserCity').value;	
		var usercountry		= $('country').value;	
		var userday			= $('UserDay').value;
		var usermonth		= $('UserMonth').value;
		var useryear		= $('UserYear').value;
		var userident		= $('UserIdent').value;
		var error_message  	= '';
		
		
		/*  VALIDATING BEFORE AJAX */
		var dateValidation = isValidDate(userday, usermonth, useryear);
		if(dateValidation == "yes"){
			$("error_StartDate").style.display="none";
			error_message += $("error_StartDate").innerHTML = "";
		}else{
			$("error_StartDate").style.display="";
			error_message += $("error_StartDate").innerHTML = dateValidation;
		}
		
		if($("UserCity").value=="City"){
			$("error_city").style.display="";
			error_message += $("error_city").innerHTML = "City Name required";
		}	
		else if(!validate_text_field($("UserCity").value, false, true, false, 3, 64)){
			$("error_city").style.display="";
			error_message += $("error_city").innerHTML = "City Name required";
		}else if(!alphnumericValidate($("UserCity").value)){
			$("error_city").style.display="";
			error_message += $("error_city").innerHTML = "Enter valid City Name";
		}else{
			$("error_city").style.display="none";
			error_message += $("error_city").innerHTML = "";
		}
		
		if($("UserState").value=="State"){
			$("error_state").style.display="";
			error_message += $("error_state").innerHTML = "State Name required";
		}
		else if(!validate_text_field($("UserState").value, false, true, false, 1, 64)){
			$("error_state").style.display="";
			error_message += $("error_state").innerHTML = "State Name required";
		}else if(!alphnumericValidate($("UserState").value)){
				$("error_state").style.display="";
			error_message += $("error_state").innerHTML = "Enter valid State Name";
		}else{
				$("error_state").style.display="none";
		error_message += $("error_state").innerHTML = "";
		}
		if(usercountry==""){error_message += $("error_country").innerHTML = "Country Required";	$("error_country").style.display="";}else{error_message += $("error_country").innerHTML = "";$("error_country").style.display="none";}
		
		/* AJAX STMT */
		if(error_message==""){
		var url 		= SiteAjaxPath+"profile_ajax.php";
		var pars		= "objAction=SaveBasicInfo&user_state="+userstate+"&user_city="+usercity+"&user_country="+usercountry+"&user_day="+userday+"&user_month="+usermonth+"&user_year="+useryear+"&user_ident="+userident;
		var myAjax 		= new Ajax.Request(url, {method:'post',postBody:pars, onSuccess:this.SaveBasicInfoCompleteJS, onFailure:this.showcontactsFailed});}
	
	},
	SaveBasicInfoCompleteJS:function(t)
	{
		var myJSONObject 	= parseJSON(t.responseText);
		var BasicInfo 		= null;
		BasicInfo 	 		= myJSONObject.UserDetails;

		new NewProfile('CancelBasicInfo');
		
		$('detail_location_info').innerHTML = BasicInfo.state+" , "+BasicInfo.city;
		$('detail_country_info').innerHTML 	= BasicInfo.CountryName;
		$('detail_dob_info').innerHTML 		= BasicInfo.Age;
		
	},
	EditLinksInfoJS:function()
	{
		/* HIDE ALL LINKS LABEL AND SHOW TEXTBOX */		
		$('link1_info').style.display 		= "none";
		$('link2_info').style.display 		= "none";
		$('link3_info').style.display 		= "none";
		$('link_btn_info').style.display 	= "none";
		
		/* TEXT BOXES DISPLAY */		
		$('link1_txtbox').style.display  = "";
		$('link2_txtbox').style.display  = "";
		$('link3_txtbox').style.display  = "";
		
		/* SAVE AND CANCEL DISPLAY */	
		$('link_btn_save').style.display 	= "";
		$('link_btn_cancel').style.display 	= "";	
	},
	SaveLinskInfoJS:function()
	{
		var error_message	= '';
		var userident		= $('UserIdent').value;
		
	
		if(!checkURL($('profile_page1').value)){
			error_message += $("error_profile_page1").innerHTML = "Enter valid Profile URL";
			//$("error_profile_links").innerHTML="";
		}else{
			error_message += $("error_profile_page1").innerHTML = "";
			//$("error_profile_links").innerHTML="";
		}
		
		if(!checkURL($('profile_page2').value)){
			error_message += $("error_profile_page2").innerHTML = "Enter valid Profile URL";
			//$("error_profile_links").innerHTML="";
		}else{
			error_message += $("error_profile_page2").innerHTML = "";
			//$("error_profile_links").innerHTML="";
		}
		
		if(!checkURL($('profile_page3').value)){
			error_message += $("error_profile_page3").innerHTML = "Enter valid Profile URL";
			//$("error_profile_links").innerHTML="";
		}else{
			error_message += $("error_profile_page3").innerHTML = "";
			//$("error_profile_links").innerHTML="";
		}
		
		/*if($('profile_page1').value=="" && $('profile_page2').value=="" && $('profile_page3').value==""){
			error_message += $("error_profile_links").innerHTML = "Enter Atleast One Profile URL";
		}*/

	if(error_message==""){
		var url 		= SiteAjaxPath+"profile_ajax.php";
		var pars		= "objAction=SaveLinksInfo&link1="+$('profile_page1').value+"&link2="+$('profile_page2').value+"&link3="+$('profile_page3').value+"&user_ident="+userident;
		var myAjax 		= new Ajax.Request(url, {method:'post',postBody:pars, onSuccess:this.SaveLinkInfoCompleteJS, onFailure:this.showcontactsFailed});}
		
	},
	SaveLinkInfoCompleteJS:function(t)
	{
		new NewProfile('CancelLinskInfo');
	},
	CancelLinskInfoJS:function()
	{
		var userident	= $('UserIdent').value;
		var url 		= SiteAjaxPath+"profile_ajax.php";
		var pars		= "objAction=CancelInfo&user_ident="+userident;
		var myAjax 		= new Ajax.Request(url, {method:'post',postBody:pars, onSuccess:this.CancelLinkInfoCompleteJS, onFailure:this.showcontactsFailed});
		new NewProfile('ClearLinksErrorDivs');
	},
	CancelLinkInfoCompleteJS:function(t)
	{
		var myJSONObject 	= parseJSON(t.responseText);
		var LinkInfo 		= null;
		LinkInfo 	 		= myJSONObject.UserDetails;
		
		$('link1_info').style.display 		= "";
		$('link2_info').style.display 		= "";
		$('link3_info').style.display 		= "";
		$('link_btn_info').style.display 	= "";
		$('link_btn_save').style.display 	= "none";
		$('link_btn_cancel').style.display 	= "none";
		
		/* TEXT BOXES DISPLAY */		
		$('link1_txtbox').style.display  = "none";
		$('link2_txtbox').style.display  = "none";
		$('link3_txtbox').style.display  = "none";
		
		$('profile_page1').value 	= LinkInfo.profile_page1;
		$('profile_page2').value	= LinkInfo.profile_page2;
		$('profile_page3').value	= LinkInfo.profile_page3;
		
		
		$('link1_info').innerHTML 		=  LinkInfo.profile_page1;
		$('link2_info').innerHTML 		=  LinkInfo.profile_page2;
		$('link3_info').innerHTML 		=  LinkInfo.profile_page3;
	},
	EditArtistInfoJS:function()
	{
		/* HIDE ALL LINKS LABEL AND SHOW TEXTBOX */		
		$('artist1_info').style.display 		= "none";
		$('artist2_info').style.display 		= "none";
		$('artist3_info').style.display 		= "none";
		$('artist_btn_info').style.display 		= "none";
		
		/* TEXT BOXES DISPLAY */		
		$('artist1_txtbox').style.display  = "";
		$('artist2_txtbox').style.display  = "";
		$('artist3_txtbox').style.display  = "";
		
		/* SAVE AND CANCEL DISPLAY */	
		$('artist_btn_save').style.display 	= "";
		$('artist_btn_cancel').style.display 	= "";	
	},
	SaveArtistInfoJS:function()
	{
		//var error_message	= '';
		var userident		= $('UserIdent').value;
		
		/*if($('fav_artist1').value=="" && $('fav_artist2').value=="" && $('fav_artist3').value==""){
			error_message += $("error_fav_artist").innerHTML = "Enter Atleast One Favourite Artist";
		}*/

	//if(error_message==""){
		var url 		= SiteAjaxPath+"profile_ajax.php";
		var pars		= "objAction=SaveArtistInfo&artist1="+$('fav_artist1').value+"&artist2="+$('fav_artist2').value+"&artist3="+$('fav_artist3').value+"&user_ident="+userident;
		var myAjax 		= new Ajax.Request(url, {method:'post',postBody:pars, onSuccess:this.SaveArtistInfoCompleteJS, onFailure:this.showcontactsFailed});//}
	},
	SaveArtistInfoCompleteJS:function(t){
		new NewProfile('CancelArtistInfo');
	},
	CancelArtistInfoJS:function(){
	var userident	= $('UserIdent').value;
		var url 		= SiteAjaxPath+"profile_ajax.php";
		var pars		= "objAction=CancelInfo&user_ident="+userident;
		var myAjax 		= new Ajax.Request(url, {method:'post',postBody:pars, onSuccess:this.CancelArtistInfoCompleteJS, onFailure:this.showcontactsFailed});
	},
	CancelArtistInfoCompleteJS:function(t)
	{
		var myJSONObject 	= parseJSON(t.responseText);
		var LinkInfo 		= null;
		ArtistInfo 	 		= myJSONObject.UserDetails;
		
		$('artist1_info').style.display 		= "";
		$('artist2_info').style.display 		= "";
		$('artist3_info').style.display 		= "";
		$('artist_btn_info').style.display 	= "";
		$('artist_btn_save').style.display 	= "none";
		$('artist_btn_cancel').style.display 	= "none";
		
		/* TEXT BOXES DISPLAY */		
		$('artist1_txtbox').style.display  = "none";
		$('artist2_txtbox').style.display  = "none";
		$('artist3_txtbox').style.display  = "none";
		
		$('fav_artist1').value 	= ArtistInfo.fav_artist1;
		$('fav_artist2').value	= ArtistInfo.fav_artist2;
		$('fav_artist3').value	= ArtistInfo.fav_artist3;
		
		
		$('artist1_info').innerHTML 		=  ArtistInfo.fav_artist1;
		$('artist2_info').innerHTML 		=  ArtistInfo.fav_artist2;
		$('artist3_info').innerHTML 		=  ArtistInfo.fav_artist3;
	},
	EditTasteInfoJS:function()
	{
		/* HIDE ALL LINKS LABEL AND SHOW TEXTBOX */		
		$('taste1_info').style.display 		= "none";
		$('taste2_info').style.display 		= "none";
		$('taste3_info').style.display 		= "none";
		$('taste4_info').style.display 		= "none";
		$('taste5_info').style.display 		= "none";
		$('taste6_info').style.display 		= "none";
		$('taste_btn_info').style.display 	= "none";
		
		/* TEXT BOXES DISPLAY */		
		$('taste1_select').style.display  = "";
		$('taste2_select').style.display  = "";
		$('taste3_select').style.display  = "";
		$('taste4_select').style.display  = "";
		$('taste5_select').style.display  = "";
		$('taste6_select').style.display  = "";
		
		/* SAVE AND CANCEL DISPLAY */	
		$('taste_btn_save').style.display 	= "";
		$('taste_btn_cancel').style.display = "";	
	},
	SaveTasteInfoJS:function()
	{
		//var error_message	= '';
		var userident		= $('UserIdent').value;
		
		/*if($('taste1').value=="" && $('taste2').value=="" && $('taste3').value=="" && $('taste4').value=="" && $('taste5').value=="" && $('taste6').value==""){
			error_message += $("error_music_taste").innerHTML = "Select Atleast One Music Taste";
		}*/

	/*if(error_message==""){*/
		var url 		= SiteAjaxPath+"profile_ajax.php";
		var pars		= "objAction=SaveTasteInfo&taste1="+$('taste1').value+"&taste2="+$('taste2').value+"&taste3="+$('taste3').value+"&user_ident="+userident+"&taste4="+$('taste4').value+"&taste5="+$('taste5').value+"&taste6="+$('taste6').value;
		var myAjax 		= new Ajax.Request(url, {method:'post',postBody:pars, onSuccess:this.SaveTasteInfoCompleteJS, onFailure:this.showcontactsFailed});//}
	},
	SaveTasteInfoCompleteJS:function(){
		
		new NewProfile('CancelTasteInfo');
	},
	CancelTasteInfoJS:function()
	{
		var userident	= $('UserIdent').value;
		var url 		= SiteAjaxPath+"profile_ajax.php";
		var pars		= "objAction=CancelInfo&user_ident="+userident;
		var myAjax 		= new Ajax.Request(url, {method:'post',postBody:pars, onSuccess:this.CancelTasteInfoCompleteJS, onFailure:this.showcontactsFailed});
	},
	CancelTasteInfoCompleteJS:function(t)
	{
		var myJSONObject 	= parseJSON(t.responseText);
		var LinkInfo 		= null;
		TasteInfo 	 		= myJSONObject.UserDetails;
		
		$('taste1_info').style.display 		= "";
		$('taste2_info').style.display  	= "";
		$('taste3_info').style.display  	= "";
		$('taste4_info').style.display  	= "";
		$('taste5_info').style.display  	= "";
		$('taste6_info').style.display  	= "";
		$('taste_btn_info').style.display 	= "";
		$('taste_btn_save').style.display 	= "none";
		$('taste_btn_cancel').style.display = "none";
		
		/* TEXT BOXES DISPLAY */		
		$('taste1_select').style.display  = "none";
		$('taste2_select').style.display  = "none";
		$('taste3_select').style.display  = "none";
		$('taste4_select').style.display  = "none";
		$('taste5_select').style.display  = "none";
		$('taste6_select').style.display  = "none";
		
		$('taste1').value 	= TasteInfo.music_taste1;
		$('taste2').value	= TasteInfo.music_taste2;
		$('taste3').value	= TasteInfo.music_taste3;
		$('taste4').value 	= TasteInfo.music_taste4;
		$('taste5').value	= TasteInfo.music_taste5;
		$('taste6').value	= TasteInfo.music_taste6;
		
		
		$('taste1_info').innerHTML 		=  TasteInfo.music_taste1;
		$('taste2_info').innerHTML 		=  TasteInfo.music_taste2;
		$('taste3_info').innerHTML 		=  TasteInfo.music_taste3;
		$('taste4_info').innerHTML 		=  TasteInfo.music_taste4;
		$('taste5_info').innerHTML 		=  TasteInfo.music_taste5;
		$('taste6_info').innerHTML 		=  TasteInfo.music_taste6;
	},
	ClearLinksErrorDivsJS:function()
	{
		$("error_profile_page1").innerHTML = "";
		$("error_profile_page2").innerHTML = "";
		$("error_profile_page3").innerHTML = "";
	},
	ClearBInfoErrorDivsJS:function()
	{
		$("error_StartDate").innerHTML = "";
		$("error_city").innerHTML = "";
		$("error_state").innerHTML = "";
		$("error_country").innerHTML = "";
	},
	CheckDuplicateMusicJS:function(){
		var music1 = $('taste1').value;
		var music2 = $('taste2').value;
		var music3 = $('taste3').value;
		var music4 = $('taste4').value;
		var music5 = $('taste5').value;
		var music6 = $('taste6').value;

		var MusicArray = [];
		if(music1!="")
			MusicArray[0] = music1;
		if(music2!="")
			MusicArray[1] = music2;
		if(music3!="")
			MusicArray[2] = music3;
		if(music4!="")
			MusicArray[3] = music4;
		if(music5!="")
			MusicArray[4] = music5;
		if(music6!="")
			MusicArray[5] = music6;

		return this.CheckMusicUniqueJS(MusicArray);
	},
	CheckMusicUniqueJS:function(A)
	{
		var i, j, n;
		var duplicates = "";
		n=A.length;
		for (i=0; i<n; i++) 
		{   
			for (j=i+1; j<n; j++) 
			{          
				if (A[i]==A[j]){
					duplicates += "'"+A[j]+"' ";					
				}
				
			}	
		}		
		return duplicates;
		
	}
	
}
