//this is for getting get varible from query string of url
//@param varible=name of the variable
//returns vlue of the variable
function getQueryVariable(variable)
{  
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++)
		{
			var pair = vars[i].split("=");
			if (pair[0] == variable) 
			{
				return pair[1];
			}
		}
//	alert('Query Variable ' + variable + ' not found');
}

//simillar to document.onload function
//this is jQuerys core function. one document can uahe more than 1 .ready function
//here we define it to show date format on two textbox on blur and onfucs show blank
//it is not need now as Dr. J requested to allow any format even simple text
$(document).ready(function(){
	
  	//find all existance of h_start/end_date and bind a calender to it
  	var i=1;
	while ( $("#h_start_date"+i).length > 0 ) {//when an object exist
/*		var d = new Date();
		var curr_year = d.getFullYear();
		$('#h_start_date'+i).datepicker({ yearRange: '1970:'+curr_year });
		$('#h_end_date'+i).datepicker({ yearRange: '1970:'+curr_year });
*/
		//blur
	    $('#h_start_date'+i).blur(function () {
		if($(this).val()=='')
		   $(this).val('Date Format mm/dd/yyyy');
	    });
	    $('#h_end_date'+i).blur(function () {
		if($(this).val()=='')
		   $(this).val('Date Format mm/dd/yyyy');
	    });
	    //focus
	    $('#h_start_date'+i).focus(function () {
		if($(this).val()=='Date Format mm/dd/yyyy')
		   $(this).val('');
	    });
	    $('#h_end_date'+i).focus(function () {
		if($(this).val()=='Date Format mm/dd/yyyy')
		   $(this).val('');
	    });
	    i++;
	}
  });

/*
* take a class name and bind click event to all the object related to that class
*/
function global_troggole(event){
	var categID = $(this).attr('id');
	var skillset = $("."+event.data.classname);
	$("#"+categID+"_").slideToggle("slow");
	for(var i=0; i<skillset.length;i++){
	if($(skillset[i]).attr('id')!=""+categID+"_")
		if($(skillset[i]).hide)
		$(skillset[i]).hide();
	}
}

// for notes we have 2 div where we need to bind click event
// one for full note history. other for important notes only
// here we implemented onclick function for all notes
function note_troggole(){
var header_value=$('#note-header').html();
	if(header_value=='Notes History (Click HERE To Show All Records)'){
		$('#note-header').html('Notes History (Click HERE To Show Recent Records Only)');
		$.post("notes.php",{ resume_id:getQueryVariable("id"),action:"show",limit:1000 } ,function(data)
			{
				$("#six-notes").html(data);
			}
		);
	}
	else if(header_value=='Notes History (Click HERE To Show Recent Records Only)'){
		$('#note-header').html('Notes History (Click HERE To Show All Records)');
		$.post("notes.php",{ resume_id:getQueryVariable("id"),action:"show",limit:6 } ,function(data)
			{
				$("#six-notes").html(data);
			}
		);
	}
}

//here we implementd the important note
function imp_note_troggole(){
	$.post("notes.php",{ resume_id:getQueryVariable("id"),action:"show",filter:" and important=1 " } ,function(data)
		{
			$("#six-notes").html(data);
		}
	);
}

//bind troggole function to skills, 3 history and notes
//see jQuery doc for more help
$(function() {
	$('.sk_categ').bind('click',{classname:'sk_categ_'}, global_troggole);
	$('.emp_his').bind('click', {classname:'emp_his_'}, global_troggole);
	$('.edu_his').bind('click', {classname:'edu_his_'}, global_troggole);
	$('.cer_his').bind('click', {classname:'cer_his_'}, global_troggole);
	$('#note-header').bind('click', note_troggole);
	$('#note-imp').bind('click', imp_note_troggole);
});
/*
* takes a name and serial number and delete any instance of nameserial from DOM
* example removeContent('employ',2) will remove the object with id 'employ2' from DOM
*/
function removeContent(name,serial) {
	if(confirm('Sure to remove this history?'))
		$("table tr").remove('#'+name+serial);
}
/*Loads content of table row via ajax call
* format: sourceUrl=employ_history.php,education_history.php,certification_history.php
*/
function loadContent(sourceUrl) {
	var i=1;
	var f3c=sourceUrl.substring(0,3);//first 3 chars i.e. emp,edu,cer
	var fp=sourceUrl.split("_",1);//first part i.e. employ,education,certification
	
	var initials='';
	if(f3c=='emp') initials='h'; else if(f3c=='edu') initials='e'; else if(f3c=='cer') initials='c';
	while(true){
		var obj1=document.getElementsByName(initials+'_title['+i+']');
		if (obj1[0] == null) {break;}
		i++;
		if(i==20) {alert("Sorry bug!!!!"); break;}
	}
	$("#msgbox_"+initials).html("<b>Please wait while Loading...</b>");
	$.get("../"+sourceUrl, { index: i },
	  function(data){
		$('#'+fp+(i-1)).after(data);

		$('#'+f3c+'_his'+i).bind('click', {classname:f3c+'_his_'}, global_troggole);
		$('#'+f3c+'_his'+i).trigger('click');

		if(f3c=='emp'){
		//this if block is not needed now are not needed noe
		var d = new Date();
		var curr_year = d.getFullYear();
			//$('#h_start_date'+i).datepicker({ yearRange: '1970:'+curr_year });//calender control
			//$('#h_end_date'+i).datepicker({ yearRange: '1970:'+curr_year });
			$('#h_start_date'+i).blur(function () {
			if($(this).val()=='')
			   $(this).val('Date Format mm/dd/yyyy');
			});
			$('#h_end_date'+i).blur(function () {
			if($(this).val()=='')
			   $(this).val('Date Format mm/dd/yyyy');
			});
			//focus
			$('#h_start_date'+i).focus(function () {
			if($(this).val()=='Date Format mm/dd/yyyy')
			   $(this).val('');
			});
			$('#h_end_date'+i).focus(function () {
			if($(this).val()=='Date Format mm/dd/yyyy')
			   $(this).val('');
			});
		}
		var pagename=fp+'_history.php';
		$("#msgbox_"+initials).html("<a href=\"javascript:loadContent('"+fp+"_history.php');\">[Click HERE to Add Another History]</a>");
	  });
}

//thus function will delete a note via ajax call to tiny_script
function delete_note(note_id)
{
	if(!confirm("Are you sure you Delete this Note")) return;
	$.post("notes.php",{ note_id:note_id,resume_id:getQueryVariable("id"),action:"del" } ,function(data)
		{
			$("#six-notes").html(data);
		}
	);
}

//add note via ajax call to tiny_script_jq.js
function add_note(admin_id)
{
	var imp=($('#cb-imp').is(':checked'))?1:0;
$.post("notes.php",{ resume_id:getQueryVariable("id"),admin_id:admin_id,action:"add",note:$("#internal_comment").val(),imp:imp } ,function(data)
		{
			$("#six-notes").html(data);
			$("#internal_comment").val("");
			$("#cb-imp").attr('checked', false);
		}
		);
}

function apply_for_job(){
	$("#div_apply_for_job").html("Pleae wait while processing...");
	$.post("tiny_scripts_jq.php",{ tag:"apply for job", job_id:$('#AllJobs').val(), resume_id:getQueryVariable("id")} ,function(data)
	{
		var id=$('#AllJobs').val();
		$("#div_apply_for_job").html(data);
	});
}

function send_email(id){
	if($('#mailtemplate').val()<=0){
		alert("No Email Template Selected");
		$('#mailtemplate').focus();
		return false;
	}
	
	$.post("tiny_scripts_jq.php",{ tag:"submit email", template_id:$('#mailtemplate').val(), mod_body:$('#template_ta').val(), resume_id:id} ,function(data)
	{
		$("#email_template_div").html("Mail Has Been Sent");
	});
}

function fetch_template(){
		$("#email_template_div").html("Wait while fetching the template...");

		$.post("tiny_scripts_jq.php",{ tag:"fetch template", template_id:$('#mailtemplate').val()} ,function(data)
		{
			$("#email_template_div").html(data);
		});
}

function stripslashes( str ) 
{ 
    return (str+'').replace(/\0/g, '0').replace(/\\([\\'"])/g, '$1');
}


function addslashes( str ) 
{
    return (str+'').replace(/([\\"'])/g, "\\$1").replace(/\0/g, "\\0");
}
