$(document).ready(function(){
 	//this send ajax request to fetch news according to clicked tag from tag cloud
	$('div.tagcloud a').click(function(){
		var val = $(this).html();
		
		val = replacevalue(val,"_", " "); //Replace the blank space with underscore in URL added by ajoshi 11-05-2010
		
		var Ajaxurl = $('#tagColudInfo').attr('value');
		
		//var LoadingText = $('#TagCloudLoadingText').attr('value');
		
		var NewVal = Ajaxurl.substring(Ajaxurl.lastIndexOf("\\")+1,Ajaxurl.lastIndexOf("\.")); // This will take the path till New-language
		NewVal+='-'+val+'.html'; // attach the tag with html extention with new value and generate new URL - News-language-tag.html
		Ajaxurl =NewVal; // New URL is assing to ajex url
		
		//Ajaxurl += 'tag/'+val;
		//DataByAjax(Ajaxurl,'#NewsDetailId',LoadingText);
		
		redirectPage(Ajaxurl);
	});

});

//Fucntion for replacing the blank space with underscore in URL added by ajoshi 11-05-2010
function replacevalue(data, replacewith, replacevalue)
{
    try
    {
        data = data.replace(replacevalue,replacewith); 
  if(data.indexOf(replacevalue) != -1)
  {
   data = removeslashes(data,replacewith,replacevalue);
  }
    }
    catch(e){}
    return data;
}
/*This function fetches data through ajax.*/
function DataByAjax(AjaxUrl,divId,loadingText)
{	
	(loadingText != '')?$(divId).html('<div class="contentloading">'+loadingText+'...</div>'):$(divId).html('');
	$(divId).load(AjaxUrl);
}

/* This function creates ajax popup*/
function JsPopup(AjaxUrl,imgpath,PopupDivId,Left,Top,loadingText)
{
	
	DataByAjax(AjaxUrl,PopupDivId,'')//call ajax
	$('body').block({ message:$(PopupDivId),css: { border: '0', 
												   marginTop:  Top+'px', 
												   marginLeft: Left+'px',
												   background: 'url("'+imgpath+'spacer.gif") repeat',
												   left: '0px', 
												   cursor:  null,
												   height: '100%', 
												   width:'795px' }})

												  
}

/* This function will submit form to server using ajax */
function submitFormAjax(method,url,dataString)
{
	$.ajax({  
	   type: method,  
	   url: url,  
	   data: dataString,  
	   success: function() {  
//	       alert("You have sucessfully submitted form, we will contact you soon.");
	   }  
	 });  
}

function validate(form,langauge)
{
	var msg = "";
	var RegEx=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
	
 	//validate
	var name = form.ct_name.value;
	if(name == "")
	{
		if (langauge == "en") {
			msg += "Please Enter name. \n";
		}
		else {
			msg += "S'il vous plaît Entrer le nom. \n";
		}
	}
	
	var surname = form.ct_surname.value;
	if(surname == "")
	{
		if (langauge == "en") {
			msg += "Please Enter surname. \n";
		}
		else {
			msg += "Entrez s'il vous plaît nom de famille. \n";
		}
		
	}
	
	var company_name = form.ct_company_name.value;
	if(company_name == "")
	{
		if (langauge == "en") {
			msg += "Please Enter Company Name. \n";
		}
		else {
			msg += "Entrez s'il vous plaît Nom de la société. \n";
		}		
	}
	
	var phone = form.ct_phone.value;
	if(phone != '' && isNaN(phone))
	{
		if (langauge == "en") {
			msg += "Please Enter valid phone number. \n";
		}
		else {
			msg += "S'il vous plaît Entrez le numéro de téléphone valide. \n";
		}
	}
	
	var email = form.ct_email.value;
	if(email == "")
	{
		if (langauge == "en") {
			msg += "Please Enter Email. \n";
		}
		else {
			msg += "Entrez une adresse email s'il vous plaît. \n";
		}
		
	}
	else if (!RegEx.test(email))
  	{
		if (langauge == "en") {
			msg += "Please input a valid email address. \n";
		}
		else {
			msg += "S'il vous plaît une entrée adresse électronique valide. \n";
		}
  	}
	
	var message = form.ct_message.value;
	if(message == "")
	{
		if (langauge == "en") {
			msg += "Please Enter message. \n";
		}
		else {
			msg += "Entrez s'il vous plaît message. \n";
		}
	}
	
	
	if(msg != "")
	{
		alert(msg);
		return '';
	}
	
	var dataString = 'name='+ name + 
 					  '&surname=' + surname + 
 					  '&company_name='+ company_name + 
 					  '&email=' + email + 
 					  '&phone=' + phone +
 					  '&message=' + message;
	return dataString;
}

function submitForm(url,langauge)
{
	
 	var form = document.getElementById('contact');
	var dataString;
	
	//validate form
	dataString = validate(form,langauge);
	if(dataString == '')
	{
		return false;
	}
						  
	 submitFormAjax("POST",url,dataString);
	
	 //close div
	 $.unblockUI();
	 
	return true;
}

/* This function will rotate data using ajax with fixed time interval */
function RotateElement(divId,url,DelayTime,cnt)
{	
	var now = new Date();
    var ts =  now.getTime();
	
	//ajax call
	$(divId).load(url,'ts='+ts);
	if (cnt == 1) //for first time it goes through if
	{
		//set cnt to 0 to not to go for each iterate thorugh if
		cnt = 0;
		//call back the function
		RotateElement(divId,url,DelayTime,cnt);
	}
	else //after first iterate for each iterate it goes through else
	{
		// call back the function accroding to delay time set
		t = setTimeout("RotateElement('" + divId + "','" + url + "'," + DelayTime + ","+ cnt +")", DelayTime);
	}
}

/*This function is called for imagegallary */
function ShowScreenshots(AjaxUrl,BackUrl,sname,basepath,backtext,loadingtext)
{
	var ContentId = '#popupContentId_'+sname;
	$(ContentId).html('<div class="popuploading">'+loadingtext+'...</div>');
	var BacklinkId = '#backlink_'+sname;
	$(BacklinkId).html('<a href="#" class="popuptoplink" onClick="backtopopup(\''+BackUrl+'\',\''+sname+'\',\''+basepath+'\',\''+loadingtext+'\');"><div class="column popuptoplinktext">'+backtext+'</div><div class="column" style="margin-top:-2px;"><img src="'+basepath+'/images/back.png" border="0" hspace="0" vspace="0" /></div><\/a>');
	$(ContentId).load(AjaxUrl);		
}

/*This function is called for go back from imagegallary */
function backtopopup(AjaxUrl,sname,basepath,loadingtext)
{
	var ContentId = '#popupContentId_'+sname;
	$(ContentId).html('<div class="popuploading">'+loadingtext+'...</div>');
	var BacklinkId = '#backlink_'+sname;
	var HeaderId = '#contentDiv_'+sname;
	$(BacklinkId).html('<img src="'+basepath+'/images/spacer.gif" border="0" hspace="0" vspace="0" height="5" width="80" />');	
	$(ContentId).load(AjaxUrl);	
}

/* This function will expand div with sliding effect*/
function expandAndSlide(AjaxUrl,currDiv,ExpandDiv,DataDiv,loadingText)
{
	slideDiv(currDiv);
	$(DataDiv).html('<div class="centerloading">'+loadingText+'...</div>');
	var currGreyDiv = '#greydiv'+currDiv;
	var currFrontDiv = '#frontdiv'+currDiv;
	
	$(currGreyDiv).animate({"height": "596px"}, 1000);
	$(currFrontDiv).animate({"height": "590px"}, 1000);
	
	$("div.hidelink div").hide();
	$("div.supportmidsep").hide();
	$(currGreyDiv).slideDown('slow');
	
	$(currFrontDiv).slideDown('slow');
	var aExpDiv = ExpandDiv.split(',');
	for(var i=0;i<aExpDiv.length;i++)
	{
		$('#greydiv'+aExpDiv[i]).slideUp("slow");
		
		$('#frontdiv'+aExpDiv[i]).slideUp("slow");
	}
	$(DataDiv).removeClass('contentcenterouterdiv');
	$(DataDiv).load(AjaxUrl);	

}

/* This function will slide service blocks*/ 
function slideDiv(id)
{
	var H1 = 176;
	var H2 = 176;
	var H3 = 176;
	var P1 = 4;
	var P2 = 12;
	var P3 = 24;
	if(id == 1)
	{
		$("div#greydiv1_1").animate({"top":""+(P1-P1)+"px"});
		$("div#greydiv2_1").animate({"top":""+(P2-1)+"px"});
		$("div#greydiv3_1").animate({"top":""+(P3-1)+"px"});
	}
	else if(id == 2)
	{
		$("div#greydiv1_1").animate({"top":""+(H1+H2+P1+P2+P3+13)+"px"});
		$("div#greydiv2_1").animate({"top":"-"+(H1+P2+P1+1)+"px"});
		$("div#greydiv3_1").animate({"top":"-"+(H2+P1+2)+"px"});
	}
	else if(id == 3)
	{
		$("div#greydiv1_1").animate({"top":""+(P2+H2+P2+2)+"px"});
		$("div#greydiv2_1").animate({"top":""+(P3+P2+H2+1)+"px"});
		$("div#greydiv3_1").animate({"top":"-"+(H1+H2+P3+8)+"px"});
	}
}

//This function will open popup window.
function openWindow(url,title)
{
	//url with tab id
	var tabId = $('#tabDBid').attr('value');
	if(!isNaN(tabId))
		url += 'tabid/'+tabId;
	
	//open popup window
	window.open(url,title,'menubar=no,width=630,height=660,toolbar=no,scrollbars=yes');
}

//redierct page
function redirectPage(url)
{
	window.location = url;
}


/*##########################################
 * Tabs
 * Starts
 * tabs.js
 *##########################################*/

//hide tab
function hideTab(id)
{
	$('#'+id).css({'display' : 'none'});
}

//show tab
function ShowTab(id)
{
	$('#'+id).css({'display' : 'block','visibility':'visible'});
}

//manage tab show and hide
function FuncTab(id,shortname,tabid)
{
	var total = ($("div.tabmaindiv ul li").size())/2;
	//default setting of tabs
	if (id == 'default') {
		for(var j = 2; j <= total; j++)
		{
			hideTab('tab_2_'+shortname+'_'+j);
			hideTab('content_'+shortname+'_'+j);
		}
		hideTab('tab_1_'+shortname+'_'+'1');
		ShowTab('content_'+shortname+'_'+'1');
		
	}
	else {
		//split id
		var ele = id.split('_');
		var ShowId = ele[0] + '_2_' + ele[2]+'_'+ ele[3]+'_'+ele[4];
		var contentId = 'content_' + ele[2]+'_'+ ele[3]+'_'+ele[4];
		if (ele[1] == 2) {
		//it will come here if some one clicks on selected tab
		//do nothing
		}
		else {
			if (ele[1] == 1) {
				//make clicked tab selected
				ShowTab(ShowId);
				hideTab(id);
				ShowTab(contentId);
				$('#tabDBid').attr('value',tabid);//get tab id to hidden variable for printing purpose
				
			}
			//deselect other tabs and related content 
			for (var i = 1; i <= total; i++) {
				var SelectedSId = 'tab_2_'+ shortname +'_' + i;
				var SelectedHId = 'tab_1_'+ shortname +'_' + i;
				var SelectedCId = 'content_'+ shortname +'_' + i;
				if (SelectedSId != ShowId) {
					hideTab(SelectedCId);
					hideTab(SelectedSId);
					ShowTab(SelectedHId);
				}
			}
		}
	}
}

 /*##########################################
 * Tabs
 * Ends
 * tabs.js
 *##########################################*/