// JavaScript Document


// Function to hide the Success messages
function hide_flashmessages(id,time) {
	Effect.Fade(id, { duration: time });
} // End of Function

// This function Shows the Divison	
function showDiv(id) {
	$(id).style.display	 = 'block';
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
} // End of Function

// This Function Deletes the Copywriters
function delete_copywriter(id,type) {
	var res  = confirm('Are you sure want to delete this '+type + ' ?') ;
	if(res == true) {
		new Ajax.Request(website_path+'admin/copywriters/delete/'+id, {
			asynchronous:true, 
			evalScripts:true, 
			onComplete:function(request, json) {
				new Effect.Fade("row_"+id);
				showDiv('delete_msg_display');
				hide_flashmessages('delete_msg_display',4);
		}});	
	}
} // End of Function

// This Function Deletes the Coupons
function delete_coupon(id) {
	var res  = confirm('Are you sure want to delete this Coupon?') ;
	if(res == true) {
		new Ajax.Request(website_path+'admin/coupons/delete/'+id, {
			asynchronous:true, 
			evalScripts:true, 
			onComplete:function(request, json) {
				new Effect.Fade("row_"+id);
				showDiv('delete_msg_display');
				hide_flashmessages('delete_msg_display',4);
		}});	
	}
} // End of Function

// Functio to get the state based on the selected Country
function getDivisions(divid, me, sld) {
	var country_id = me.value;
	var region_sld = $('CopywriterRegionSelected').value;
	if(country_id != '0') {
		new Ajax.Updater(divid,website_path+'copywriters/getdivisions_regions/'+country_id+'/'+sld+'/'+region_sld, {
			asynchronous:true, 
			evalScripts:true, 
			onComplete:function(request, json) {
				$('regions_id').innerHTML = "";

				var country_id = $('CopywriterCountryId').value;
				var division_id = $('CopywriterDivisionId').value;
				var region_sld = $('CopywriterRegionSelected').value;

				new Ajax.Updater("regions_id",website_path+'copywriters/getregions/'+division_id+'/'+country_id+'/'+region_sld, {
					asynchronous:true,
					evalScripts:true,
					onComplete:function(request, json) {
					}
				});
			}
		});
	}
}

// Functio to get the state based on the selected Country
function getDivisions_Search(divid, me, sld) {
	var country_id = me.value;
	if(country_id != '0') {
		new Ajax.Updater(divid, website_path+'copywriters/getdivisions_regions_search/'+country_id+'/'+sld, {
			asynchronous:true, 
			evalScripts:true, 
			onComplete:function(request, json) {
				$('regions_id').innerHTML = "";
					
				var division_id = $('SearchDivisionId').value;
				new Ajax.Updater("regions_id",website_path+'copywriters/getregions_search/'+division_id+'/'+country_id, {
					asynchronous:true,
					evalScripts:true,
					onComplete:function(request, json) {
					}
				});
			}
		});
	} else {
		$(divid).innerHTML = '';	
	}
	
	
} // End of Function 

// Function to get the Experience to based on the selected Experience from
function getExperience(divid, me) {
	
	var exp_from_id = me.value;
	if(exp_from_id != '0') {
		new Ajax.Updater(divid,website_path+'copywriters/getexperience/'+exp_from_id, {
			asynchronous:true,
			evalScripts:true,
			onComplete:function(request, json) {
				}
			}
		)
	}
}

// Function to get the Experience to based on the selected Experience from
function getExperience_new(divid, me) {
	var exp_to_id = me.value;
	if(exp_to_id != '0') {
		new Ajax.Updater(
			divid,
			website_path+'copywriters/getexperience_new/'+exp_to_id,
			{
				asynchronous:true,
				evalScripts:true,
				onComplete:function(request, json) {
				}
			}
		)
	}
}

// Functio to get the state based on the selected Country
function getRegions(divid, me, sld) {

	var division_id = me.value;
	var country_id = $('CopywriterCountryId').value;
	if(division_id != '0') {
		new Ajax.Updater(divid,website_path+'copywriters/getregions/'+division_id+'/'+country_id+'/'+sld, {
			asynchronous:true, 
			evalScripts:true, 
			onComplete:function(request, json) {
			}
			}
		)
	}	
}

// Functio to get the state based on the selected Country
function getRegions_Search(divid, me, sld) {

	var division_id = me.value;
	var country_id = $('SearchCountryId').value;
	
	if(division_id != '') {
		new Ajax.Updater(divid,website_path+'copywriters/getRegions_Search/'+division_id+'/'+country_id+'/'+sld, {
			asynchronous:true, 
			evalScripts:true, 
			onComplete:function(request, json) {
				//alert('test message');
				//alert(json);
				}
			}
		)
	} // End of IF
	/*else {
		new Ajax.Updater(divid,website_path+'copywriters/getRegions_Search/'+division_id+'/'+country_id+'/'+sld, {
			asynchronous:true, 
			evalScripts:true, 
			onComplete:function(request, json) {
				//alert('test message');
				//alert(json);
			}
			}
		)
	}*/
	
	
} // End of Function 

// Functio to get the state based on the selected Country in Search of CP listing page
function getStateBox(divid,me) {
	var country_id = me.value ;
	//alert(divid);
	if(country_id != '0') {
		//alert($('').innerHTML);
		$(divid).innerHTML = '';			
		new Ajax.Updater(divid,website_path+'copywriters/getsearchstates/'+country_id, {
			asynchronous:true, 
			evalScripts:true, 
			onComplete:function(request, json) {
			}
			}
		)			
	} // End of IF
	else {
		$(divid).innerHTML = '';	
		makeDropdown(divid); // Creates the State dropdown
	}
	
	
} // End of Function 

// This function will make drop down for the State
function makeDropdown(divid) {
	// Variables for Select Box
	var box = '';
	var opt_str = '' ;
	var boxname = 'data[Search][state_id]'; // Name of Select Box
	var boxid = 'SearchStateId';	 // Id of Select Box
	opt_str += '<option value="0" selected=selected>Select Region</option>';
	
	// Make Select Box
	box += '<select class="select-small" title="Select Region" name="'+boxname+'" id="'+boxid +'" disabled="disabled">';
	box += opt_str;	
	box += '</select>';	
	//alert(box);
	
	// Now assign to InnerHTML
	$(divid).innerHTML = box;
	
} // End of Function

// This function will confirm the New Applcation.
function confirmCP(me) {
	var res = confirm("Are you sure want to confirm this application ? ");
	if(res == true) {
		me.form.submit();
	} else {
		$('CopywriterStatus').checked = false;	
		return false;
	}
} // End of Function

function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{
			c_start = c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}
