/*---------Reza-------------*/
//global variables:
var curSrc='', curMainNavIndex=-1, curSubNavIndex=-1;
var window950Left=0, window950Top=0, ddWidth=0;
var mainNavLeftPos = new Array(), subNavTopPos = new Array();
var newSlideDownBg=$('#slideDownBg1'), curSlideDownBg=$('#slideDownBg2');
var onFocusVal='', careerPost='';

var ANIMATING=false, SUBANIMATING=false, DOUBLEPOPUP=false;

//set the animation speed default values:
var eyesOnAfterTimeMin=100, eyesOnAfterTimeMax=400;
var eyesOffAfterTimeMin=100, eyesOffAfterTimeMax=2500;
var eyesOnAfterTime, eyesOffAfterTime, twitterBlinkEyesOnID, twitterBlinkEyesOffID;
var ANIMSPEED=1200;

//returns a random integer between & including min to max:
function randomTime(Min, Max){return Math.floor(Math.random() * (Max - Min + 1)) + Min}

var backImages= new Array('back01.jpg','back02.jpg','back03.jpg','back04.jpg','back05.jpg','back06.jpg','back07.jpg','back08.jpg','back09.jpg','back10.jpg','back11.jpg','back12.jpg'); /*Add as many*/

var preAjaxLoaderImg = document.createElement('img');
preAjaxLoaderImg.src = 'images/interface/ajax-loader.gif';

function preLoadBgImages(){
	var pre_img_obj = new Array(backImages.length);
	for(i=0; i<backImages.length; ++i){
		pre_img_obj[i] = document.createElement('img');
		pre_img_obj[i].src = 'images/backgrounds/'+backImages[i];
	}
}

function preLoadCustomHoverImages(){
	var pre_thumb_obj = new Array(backImages.length);
	for(i=0; i<backImages.length; ++i){
		pre_thumb_obj[i] = document.createElement('img');
		pre_thumb_obj[i].src = 'images/backgrounds/thumbs-colorized/'+backImages[i];
	}
}

$(document).ready(function(){
	//Initialize position:
	twitterBlinkEyesOff();
	initializeCustomize();
	windowResizing();
	customCheckBox();
	getMainNavLeftPos();
	
	$(window).bind("resize", function(){windowResizing()});
	
	$('#customize img').hover(
		function(){
			curSrc = $(this).attr('src');
			$(this).attr('src', curSrc.replace("thumbs-saturated", "thumbs-colorized")).css('border-color','#ccc');
		},
		function(){$(this).attr('src', curSrc).css('border-color','');}
	);
	
	$('#mainNavDetailList .gridCell5 li').hover(
		function(){
			if($(this).find('dl.description dd').is(':hidden'))
				$(this).find('img').fadeTo("fast", 0.5).parent("li").find('dl.description dd:first').slideDown("medium");
		},
		function(){
			if($(this).find('dl.description dd').is(':visible'))
				$(this).find('img').fadeTo("fast", 1.0).parent("li").find('dl.description dd:first').slideUp("medium");
		}
	);
	
	$('.subRightContainer li a').hover(
		function(){$(this.parentNode).find('img').show()},
		function(){$(this.parentNode).find('img').hide()}
	);
	
	$('#featureList li').hover(
		function(){if(!$(this).hasClass('selected')) $(this).find('img').fadeTo("fast", 1.0)},
		function(){if(!$(this).hasClass('selected')) $(this).find('img').fadeTo("fast", 0.5)}
	);
	
	$('#customize img').click(function(){
		var index = $('#customize img').index(this);
		var curSrc = $('#customize img').eq(index).attr('src');
		var newSrc = curSrc.replace("thumbs-colorized/", "");
		$('body').css('background-image','url('+newSrc+')');
		customize();
	});
	
	$('input:text, textarea').focus(function(){
		$(this).addClass('focused');
		if($(this).hasClass('mandatoryErr') && (this.value.indexOf('We Really Need')!=-1 || this.value.indexOf('Please complete the field')!=-1 || this.value.indexOf('Please enter a valid Email address')!=-1)){
			onFocusVal=$(this).val();
			$(this).val('');
		}
	}).blur(function(){
		$(this).removeClass('focused');
		if($(this).hasClass('mandatoryErr') && $(this).val()==''){
			$(this).val(onFocusVal);
		}
	});
	
	$('#featureList li').click(function(){
		var index= $('#featureList li').index(this);
		$('#featureList li').removeClass('selected').find('img').fadeTo("fast", 0.5);
		$(this).addClass('selected').find('img').fadeTo("fast", 1.0);
		if($('#featureDetailsList li:eq('+index+')').is(':hidden'))
			$('#featureDetailsList li').hide().eq(index).fadeIn('slow');
	});
	
	$("#mainNavList > li > a").click(function(){
		var index=$('#mainNavList > li > a').index(this);
		if(!(ANIMATING || SUBANIMATING || index==curMainNavIndex)){
			//no more same event while one is animating:
			ANIMATING=true;
			
			//initialize variables:
			var temp1,temp2, sliderOfsetLeft=-1, gapLeft=10, gapRight=12;
			curMainNavIndex=index;
			curSubNavIndex=0; //resetting each time to the first list;
			if($('#slideDownBg1').is(':hidden')){temp1=1; temp2=2} else {temp1=2; temp2=1}
			newSlideDownBg=$('#slideDownBg'+temp1);
			curSlideDownBg=$('#slideDownBg'+temp2);
			
			//animStep1:
			$('#specialAnnouncements:visible').slideUp('slow');
			$('#pressReleases:visible').fadeOut(ANIMSPEED);
			$('#featureContainer:visible').fadeOut(ANIMSPEED);
			$('ul.subNavList:visible').hide();
			$('#vMiddleIn:visible').removeClass('mainNavDetailListShow');
			
			//animStep2:
			$('ul#mainNavList > li').removeClass("clicked").eq(curMainNavIndex).addClass("clicked");
			for(var i=0; i<$('ul#mainNavList > li').length; i++){
				if($.browser.msie && $.browser.version <=6)//png shaking after moving only:
					$('ul#mainNavList > li:eq('+i+')').animate({left: mainNavLeftPos[i]-1-(i<=curMainNavIndex? (gapLeft+351+gapRight) : 0)+'px'},ANIMSPEED).animate({left: mainNavLeftPos[i]-(i<=curMainNavIndex? (gapLeft+351+gapRight) : 0)+'px'},ANIMSPEED);
				else
					$('ul#mainNavList > li:eq('+i+')').animate({left: mainNavLeftPos[i]-(i<=curMainNavIndex? (gapLeft+351+gapRight) : 0)+'px'},ANIMSPEED);
			}
			sliderOfsetLeft=$(this).parent("li").offset().left;
			if(curSlideDownBg.offset().left < sliderOfsetLeft)
				sliderOfsetLeft+=parseInt($(this).parent("li").outerWidth())-(gapLeft+351);
			else
				sliderOfsetLeft+=parseInt($(this).parent("li").outerWidth())+(gapLeft);
			curSlideDownBg.animate( {top: $('#wrapFtr').offset().top+'px'},ANIMSPEED*3/4);
			newSlideDownBg.css({'left': sliderOfsetLeft +'px'}).slideDown(ANIMSPEED,function(){
				
				//animStep3:
				curSlideDownBg.css({'top':0,'display':'none'});
				setSubNavTopPos(0);
				$('ul#mainNavList > li:eq('+curMainNavIndex+') ul.subNavList').css('top','-27px').fadeIn(ANIMSPEED/2);
				$('ul#mainNavList > li:eq('+curMainNavIndex+') ul.subNavList').find('li').css({'font-size':'14px', 'line-height':'17px'}).removeClass("showing").eq(curSubNavIndex).addClass("showing").animate({fontSize: "24px",lineHeight:"30px"},ANIMSPEED/2);
				
				//animStep4:
				$('#vMiddleIn:hidden').addClass('mainNavDetailListShow');
				$('ul#mainNavDetailList > li.mainSection').find('.subSection').andSelf().hide();
				$('ul#mainNavDetailList > li.mainSection:eq('+curMainNavIndex+')').show().find('.subSection:first').fadeIn(ANIMSPEED/2);
				
				makeFormsActive();
				
				ANIMATING=false;
				resetExternalData();
			});
		}
	});
	
	$('#mainNavList li li a').click(function(){
		var index=$('#mainNavList li ul.subNavList:visible li a').index(this);
		if(!(ANIMATING || SUBANIMATING || index==curSubNavIndex)){
			//no more same event while one is animating:
			SUBANIMATING=true;
			
			//initialize variables:
			curSubNavIndex=index;
			
			$('ul#mainNavList li ul.subNavList:visible').animate({top: -(curSubNavIndex*17)-27+'px'},ANIMSPEED);
			setSubNavTopPos(1);
			$('ul#mainNavList li ul.subNavList:visible li').removeClass("showing").animate({fontSize:"14px",lineHeight:"17px"},{queue:false,duration:ANIMSPEED/2}).eq(curSubNavIndex).addClass("showing").animate({lineHeight:"30px",fontSize:"24px"},ANIMSPEED/2);
			
			$('ul#mainNavDetailList li.mainSection').find('.subSection').andSelf().hide();
			$('ul#mainNavDetailList li.mainSection:eq('+curMainNavIndex+')').show().find('.subSection:eq('+curSubNavIndex+')').fadeIn(ANIMSPEED, function(){
				
				//stepLast:
				SUBANIMATING=false;
				resetExternalData();
			});
		}
	});
	
	$('#customize dt').click(function(){customize()});
	
	$('.newsList li a').click(function(){
		if(!(ANIMATING || SUBANIMATING)){
			ANIMATING=true;
			var index=$('.subPeople li a').index(this);
			popupToShow('#newsDetails');
			
			$('#allPopUps').fadeIn();
			newsDetailsListWithHeader(this);
		}
	});
	
	$('.subPeople li a').click(function(){
		if(!(ANIMATING || SUBANIMATING)){
			ANIMATING=true;
			var index=$('.subPeople li a').index(this);
			popupToShow('#peopleEnlarge');
			showExternalHtml(index, 'peopleEnlarge');
		}
	});
	
	$('.gridCell5 li').click(function(){
		if(!(ANIMATING || SUBANIMATING)){
			ANIMATING=true;
			var index=$('.gridCell5 li').index(this);
			popupToShow('#featureEnlarge');
			showExternalHtml(index, 'featureEnlarge');
		}
	});
	
	$('.subCareers li a').click(function(){
		if(!(ANIMATING || SUBANIMATING)){
			ANIMATING=true;
			var index=$('.subCareers li a').index(this);
			//assign the career posts available:
			if($.browser.msie) careerPost=$(this).html().split(/<SMALL>/)[0];
			else careerPost=$(this).html().split(/<small>/)[0];
			popupToShow('#careerEnlarge');
			resetForm('careerApplyFormCenter');
			
			$('ul#careerEnlargeList > li').hide().eq(index).show();
			$('#allPopUps').fadeIn(function(){
				ANIMATING=false;
			});
		}
	});
	
	$('#specialAnnouncements .hdr').click(function(){$('#specialAnnouncementsContainer').slideToggle('slow')});
	$('#allPopUps .butn_close').click(function(){butnClose()});
 });

function resetExternalData(){
	//loading is started by AJAX-Call:
	if($('#featureEnlarge').length == 0 && curMainNavIndex==1 && curSubNavIndex==0) ajaxCall('featureEnlarge');
	else if($('#featureEnlarge').length > 0) $('#featureEnlarge').remove();
	
	if($("#peopleEnlarge").length == 0  && curMainNavIndex==2 && curSubNavIndex==1) ajaxCall('peopleEnlarge');
	else if($("#peopleEnlarge").length > 0) $('#peopleEnlarge').remove();
}

function bindClickPeople(){
	$('#peopleEnlarge .butn_close').click(function(){butnClose()});
	$('ul#peopleBox li').click(function(){
		if(!(ANIMATING || SUBANIMATING)){
			ANIMATING=true;
			var index=$('ul#peopleBox li').index(this);
			
			//loading is started by AJAX-Call as required:
			if($("ul#peopleDetails li:eq("+index+")").html()=='')
				ajaxCall('peopleEnlarge','',index);
			else
				showPeoples(index);
		}
	});
}

function bindClickFeature(){
	$('#featureEnlarge .butn_close').click(function(){butnClose()});
	
	$('#featureEnlarge .butn_next').click(function(){
		if(!(ANIMATING || SUBANIMATING)){
			ANIMATING=true;
			var curIndex=$('#featureEnlarge ul:first > li').index($('#featureEnlarge ul:first > li:visible'));
			var totIndex=$('#featureEnlarge ul:first > li').length-1;
			//only to fix ie err (causes multiple loading...):
			$('#featureEnlarge ul:first > li:visible').html('');
			if(curIndex < totIndex) showExternalHtml(curIndex+1, 'featureEnlarge');
		}
	});
	
	$('#featureEnlarge .butn_last').click(function(){
		if(!(ANIMATING || SUBANIMATING)){
			ANIMATING=true;
			var curIndex=$('#featureEnlarge ul:first > li').index($('#featureEnlarge ul:first > li:visible'));
			//only to fix ie err (causes multiple loading...):
			$('#featureEnlarge ul:first > li:visible').html('');
			if(curIndex > 0) showExternalHtml(curIndex-1, 'featureEnlarge');
		}
	});
}

function popupToShow(id){
	$('#allPopUpsInner').children("li").css({'display':'none','visibility':'hidden'});
	$(id).css({'display':'block','visibility':'visible'});
	$('#allPopUps').css('visibility','visible');
	
	//reset css as it is not hidden initially;
	if($('#allPopUps').css('top') !='0px')
		$('#allPopUps').css('top','0px').hide();
}

function showFeatures(index){
	$('#featureEnlarge ul > li').css({'display':'none','visibility':'hidden'});
	$('#featureEnlarge ul > li:eq('+index+')').css({'display':'block','visibility':'visible'});
	$('#allPopUps').fadeIn('slow',function(){
		checkActive(index);
		ANIMATING=false;
	});
}

function showPeoples(index){
	if($.browser.msie && $.browser.version <=8)
		$('ul#peopleDetails li:eq('+index+') img').show(function(){
			$('ul#peopleDetails li').css({'display':'none','visibility':'hidden'});
			$('ul#peopleDetails li:eq('+index+')').css({'display':'block','visibility':'visible'});
		});
	else
		$('ul#peopleDetails li').hide().eq(index).fadeIn();
	
	$('#allPopUps').fadeIn(function(){
		ANIMATING=false;
	});
}

function showExternalHtml(index, id){
	//loading is started by AJAX-Call as required:
	if(id=='featureEnlarge'){
		if($("#featureEnlarge ul:first > li:eq("+index+")").html()=='')
			ajaxCall('featureEnlarge','',index);
		else
			showFeatures(index);
	}
	else if(id=='peopleEnlarge'){
		if($("ul#peopleDetails li:eq("+index+")").html()=='')
			ajaxCall('peopleEnlarge','',index);
		else
			showPeoples(index);
	}
}

function butnClose(){
	if(!(ANIMATING || SUBANIMATING)){
		ANIMATING=true;
		if(!DOUBLEPOPUP){
			if($('#featureEnlarge').is(':visible'))
				$('#featureEnlarge ul:first > li:visible').html('');
			
			$('#allPopUps').fadeOut('slow', function(){
				$('#allPopUpsInner > li').css({'display':'none','visibility':'hidden'});
				$(this).css({'display':'none','visibility':'hidden'});
				ANIMATING=false;
			});
			
			if($.browser.msie && $.browser.version <=8 && $('ul#peopleDetails').length > 0)
				$('ul#peopleDetails li img').css('display','none');
			if(careerPost!=''){
				resetForm('careerApplyFormCenter');
				careerPost='';
			}
		}
		else{
			$('#careerApplyForm').fadeOut('slow',function(){
				$(this).css({'display':'none','visibility':'hidden'});
				makeFormsActive();
				ANIMATING=DOUBLEPOPUP=false;
			});
		}
	}
}

function checkActive(index){
	if((index+1)==$('.gridCell5 li').length)
		$('#featureEnlarge .butn_next').hide();
	else
		$('#featureEnlarge .butn_next').show();
		
	if(index==0)
		$('#featureEnlarge .butn_last').hide();
	else
		$('#featureEnlarge .butn_last').show();
}

var isResizing=false;	// and the body scrollpane
function windowResizing(){
	// IE triggers the onResize event internally when you do the stuff in this function
	// so make sure we don't enter an infinite loop and crash the browser;
	// At a time no 2-execution due to "isResizing=false";
	if (!isResizing) { 
		isResizing = true;
		
		//reset global variables:
		window950Left=$('#window950').offset().left;
		window950Top =$('#window950').offset().top;
		
		//call Fn(s) dependent on this Fn:
		if($('#scrollMe').length >0) customScroll();
		if(!$('#specialAnnouncements').is(':hidden')) initializeSpecialAnnouncements();
		if(!$('#pressReleases').is(':hidden')) slideDownBg('#pressReleases');
		if(curMainNavIndex!=-1) setSlideBgPos();
		
		isResizing = false;
	}
}

function customScroll(){
	$w = $('#newsSingleContainer'); //parent of scroll div;
	$c = $('#scrollMe'); //scroll div;
	var p = (parseInt($c.css('paddingLeft')) || 0) + (parseInt($c.css('paddingRight')) || 0);
	$w.children('.jScrollPaneContainer').css({'height': $w.height() + 'px', 'width': $w.width() + 'px'});
	$c.css({'height': ($w.height()-p) + 'px', 'width': ($w.width() - p) + 'px', 'overflow':'auto'});
	$c.jScrollPane({showArrows:true,scrollbarWidth:14, animateTo:true});
}

function getMainNavLeftPos(){
	for(var i=0, len=$('#mainNavList > li').length; i<len; i++)
		mainNavLeftPos[i]=parseInt($('#mainNavList > li:eq('+i+')').css('left'));
}

function setSubNavTopPos(anim){
	var allSubNav=$('#mainNavList > li:eq('+curMainNavIndex+') > ul.subNavList > li');
	for(var i=0, temp=0; i<allSubNav.length; i++){
		subNavTopPos[i]=temp+=17;//parseInt($(allSubNav[i]).outerHeight());
		if(anim==1)
			$(allSubNav[i]).animate({top: subNavTopPos[i]+'px'},ANIMSPEED);
		else
			$(allSubNav[i]).css('top', subNavTopPos[i]+'px');
		if(i==curSubNavIndex) temp+=324;
	}
}

function customCheckBox(){
	// check for what is/isn't already checked and match it on the fake ones:
	$("ul.fakeCheckBox > li input:checkbox").each(function(){
		(this.checked) ? $(this).parent("li").addClass('fakechecked') : $(this).parent("li").removeClass('fakechecked');
	});
	
	// function to toggle the checkboxes:
	$("ul.fakeCheckBox > li").click(function(){
		$(this).toggleClass('fakechecked');
		$(this).find("input:checkbox").attr('checked', !$(this).find("input:checkbox").attr('checked'));
		return false;
	});
}

function slideDownBg(container, optional){
	if(container==='#pressReleases'){
		if(optional===undefined)
			$('#slideDownBg1').css({'left':window950Left+'px','top': window950Top+'px'});
		else
			$('#slideDownBg1').css({'left':window950Left+'px', 'top':0}).slideDown(ANIMSPEED*5/4, function(){$(this).animate( { top: window950Top+'px' } , ANIMSPEED*5/4 );});
	}
}

function setSlideBgPos(){
	var This = $('ul#mainNavList > li:eq('+curMainNavIndex+')');
	newSlideDownBg.css('left', $(This).offset().left+$(This).outerWidth()+10 +'px');
}

function newsDetailsListWithHeader(obj){
	var index = $(obj.parentNode.parentNode).find('li a').index(obj);
	$('#newsDetailsListWithHeader li').hide().eq(index).show();
	customScroll();
	$('#newsDetailsListWithHeader li').eq(index).hide().show('slow', function(){
		ANIMATING=false;
	});
}

function initializeSpecialAnnouncements(){
	$('#specialAnnouncements').css({'right': window950Left+'px'});
	$('#specialAnnouncements').animate( { top: 0 } , ANIMSPEED/2 );
}

function initializeCustomize(){
	ddWidth = $('#customize dd img').length * 50; //$('#customize dd:first').outerWidth();
	var dtWidth=$('#customize dt:first').outerWidth();
	$('#customize').css({'width':(ddWidth+dtWidth)+'px','left': -(ddWidth+dtWidth+2)+'px', 'right':'auto'});
	customize(1);
}

function customize(firstTime){
	if($('#customize').css('left')=='0px' || firstTime!=undefined)
		$('#customize').animate( { left: -(ddWidth+2)+'px' } , ANIMSPEED/2 );
	else
		$('#customize').animate( { left: 0 } , ANIMSPEED/2 );
}

function twitterBlinkEyesOn(){
	//eyes are On by css:
	$('a#twitter').parent().removeClass('twitterBlinkEyesOff');
	//clear timer itself:
	clearInterval(twitterBlinkEyesOnID);
	//now activate timer to reClose eyes after a time period:
	eyesOffAfterTime= randomTime(eyesOffAfterTimeMin, eyesOffAfterTimeMax);
	twitterBlinkEyesOffID=setInterval('twitterBlinkEyesOff()', eyesOffAfterTime);
}

function twitterBlinkEyesOff(){
	//eyes are Off by css:
	$('a#twitter').parent().addClass('twitterBlinkEyesOff');
	//clear timer itself:
	clearInterval(twitterBlinkEyesOffID);
	//now activate timer to reOpen eyes after a time period:
	eyesOnAfterTime= randomTime(eyesOnAfterTimeMin, eyesOnAfterTimeMax);
	twitterBlinkEyesOnID=setInterval('twitterBlinkEyesOn()', eyesOnAfterTime);
}

function showCareerApplyForm(){
	DOUBLEPOPUP=true;
	$('#careerApplyForm').css('visibility','visible').fadeIn("slow");
}

function get_string_between(s,prefix, suffix){
	var i = s.indexOf(prefix);
	if (i >= 0)
		s = s.substring(i + prefix.length);
	else
		return '';
	
	if (suffix) {
		i = s.indexOf(suffix);
		if (i >= 0)
			s = s.substring(0, i);
		else
			return '';
	}
	return s;
}

function cleanHtml(str){
	str=$.trim(str);//remove the leading and trailing whitespace only
	//str=str.replace(/ /g,'');//remove all the spaces
	return str.replace(/[\n\r\t]/g,'');
}

function checkIframeLoading(index, newHTML){
	var iframe=document.getElementById('temporaryLoad').getElementsByTagName('iframe')[0];
	if(iframe){
		iframe.onload = iframe.onreadystatechange = function(){
			if( this.readyState && this.readyState !== "complete" && this.readyState !== "loaded" ){return;}
			// The loading is complete, call the function we want to execute once:
			$.timer(1000,function(){
				//alert('in:-> Iframe is loaded though delay a while :)');
				$("#temporaryLoad").html('');
				$("#featureEnlarge ul:first li:eq("+index+")").html(newHTML);
			});
		}
	}
}




//==============::FORM VALIDATIONs && AJAX-Call::====================
function checkEmail(email){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if(filter.test(email)) return true; else return false;
}

function chkValidity(id){
	var formName = document.getElementById(id);
	var reqFields = $('#'+id+' .mandatory');
	var err=false;
	//check for empty fields:
	for(i=0; i < reqFields.length; i++){
		if(reqFields[i].value=='' || reqFields[i].value.indexOf('We Really Need')!=-1 || reqFields[i].value.indexOf('Please complete the field')!=-1 || reqFields[i].value.indexOf('Please enter a valid Email address')!=-1){
			err=true;
			if(reqFields[i]===formName.name)
				reqFields[i].value='We Really Need Your Name';
			else if(reqFields[i]===formName.phone)
				reqFields[i].value='We Really Need Your Number';
			else if(reqFields[i]===formName.email)
				reqFields[i].value='We Really Need Your Email Address';
			
			else if(reqFields[i]===formName.fname)
				reqFields[i].value='We Really Need Your First Name';
			else if(reqFields[i]===formName.lname)
				reqFields[i].value='We Really Need Your Last Name';
			else if(reqFields[i]===formName.cletter)
				reqFields[i].value='We Really Need Your Cover Letter';
			else if(reqFields[i]===formName.city)
				reqFields[i].value='We Really Need Your City';
			else if(reqFields[i]===formName.address)
				reqFields[i].value='We Really Need Your Address';
			else if(reqFields[i]===formName.state)
				reqFields[i].value='We Really Need Your State';
			else if(reqFields[i]===formName.zipcode)
				reqFields[i].value='We Really Need Your Zipcode';
			else if(reqFields[i]===formName.resume)
				reqFields[i].value='We Really Need Your Resume';
			else
				reqFields[i].value='Please complete the field';
			$(reqFields[i]).addClass('mandatoryErr');
		}
		else $(reqFields[i]).removeClass('mandatoryErr');
	}
	if(err) return false;
	
	//check valid email:
	if(!checkEmail(formName.email.value)){
		$(formName.email).addClass('mandatoryErr');
		formName.email.value='Please enter a valid Email address';
		return false;
	}
	
	//as the form is valid do the followings:
	ajaxCall(id, formName);
	return false;
}

function makeFormsActive(){
	if($('#contact_form .thankYou').css('display')!='none'){
		$('#contact_form .thankYou').css('display','none');
		$('#contact_form .formLeft, #contact_form .subRightContainer').css('display','');
	}
	if($('#careerApplyFormCenter .thankYou').css('display')!='none'){
		$('#careerApplyFormCenter .thankYou').css('display','none');
		$('#careerApplyFormCenter .careerForms').css('display','');
	}
}

function resetForm(id){
	//clear fields:
	$('#'+id+' input:text, #'+id+' textarea').val('');
	$('#'+id+' input:checkbox').attr('checked', false).parent('li').removeClass('fakechecked');
	$('#'+id+' .mandatoryErr').removeClass('mandatoryErr');
}

function showThanks(id, formName){
	//reset thankYou fields:
	$('#'+id+' .thankYou li span').remove();
	$('#'+id+' .thankYou li strong').css('display','none');
	
	//set thankYou fields:
	if(id==='contact_form'){
		$('#'+id+' li.thankUname').append("<span>"+formName.name.value+"</span>");
		$('#'+id+' li.thankUcompany').append("<span>"+formName.company.value+"</span>");
		$('#'+id+' li.thankUphone').append("<span>"+formName.phone.value+"</span>");
		$('#'+id+' li.thankUemail').append("<span>"+formName.email.value+"</span>");
		$('#'+id+' li.thankUmsg').append("<span>"+formName.msg.value+"</span>");
	}
	else if(id==='careerApplyFormCenter'){
		$('#'+id+' li.thankUname').append("<span>"+formName.fname.value+"</span> <span>"+formName.lname.value+"</span>");
		$('#'+id+' li.thankUaddress').append("<span>"+formName.address.value+"</span>");
		$('#'+id+' li.thankUctStZip').append("<span>"+formName.city.value+",</span><span>"+formName.state.value+",</span><span>"+formName.zipcode.value+"</span>");
		$('#'+id+' li.thankUemail').append("<span>"+formName.email.value+"</span>");
	}
	
	//display Thanks:
	$('#'+id+' .formLeft, #'+id+' .subRightContainer, #'+id+' .careerForms').fadeOut(ANIMSPEED/2, function(){
		resetForm(id);
		$('#'+id+' .thankYou').fadeIn(ANIMSPEED/2);
	});
}

function ajaxCall(id, formName, index){
	parms = 'id=' + id;
	if(formName===undefined || formName==''){
		if(index!=undefined){
			var temporaryIMG='';
			if(id==='featureEnlarge'){
				temporaryIMG='<img src="'+preAjaxLoaderImg.src+'" alt="" style="display:block; margin:200px auto 300px;" />';
				$("#featureEnlarge ul:first li:eq("+index+")").html(temporaryIMG);
				showFeatures(index);
			}
			else if(id==='peopleEnlarge'){
				temporaryIMG='<img src="'+preAjaxLoaderImg.src+'" alt="" style="display:block; position:relative; left:50px; top:160px; margin:0 auto" />';
				$("#peopleDetails li:eq("+index+")").html(temporaryIMG);
				showPeoples(index);
			}
		}
	}
	else if(id==='careerApplyFormCenter' && careerPost!='')
		parms+='&appliedPost='+careerPost+'&'+jQuery(formName).serialize();
	else
		parms+='&'+jQuery(formName).serialize();
	
	$.ajax({
	   type: "POST",
	   url: "includes/php/ajax.php",
	   data: parms,
	   error: function(){
			alert('Error while loading....');
		},
	   success: function(msg){
			if(msg==1){
				//alert('mail sent');
				if(id==='contact_form' || id==='careerApplyFormCenter'){
					showThanks(id, formName);
				}
			}
			else if(msg==0)
				alert('mail not sent');
			else if(msg!=''){//Get HTML by request:
				if(id==='featureEnlarge'){
					pattern = /<li>(.*?)<\/li>/g;
					
					if(index===undefined){//just get structure:
						var newstring=msg.replace(pattern,'<li></li>');
						$("#allPopUpsInner").append(newstring);
						$.timer(200,function(){
							bindClickFeature();
						});
					}
					else{//as structure is ready, just add new content inside of target li-tag:
						var newstringArr=msg.match(pattern);
						var newHTML=get_string_between(newstringArr[index],'<li>','</li>');
						$("#temporaryLoad").html(newHTML);
						checkIframeLoading(index,newHTML);
					}
				}
				else if(id==='peopleEnlarge'){
					pattern2 = /<li>(.*?)<\/li>/g;
					var splitBY='<ul id="peopleBox">';
					var splitArr=msg.split(splitBY);
					var newstring=cleanHtml(splitArr[0]);
					
					if(index===undefined){//just get structure:
						newstring=newstring.replace(pattern2,'<li></li>');
						newstring=newstring.concat(splitBY,splitArr[1]);
						$("#allPopUpsInner").append(newstring);
						$.timer(200,function(){
							bindClickPeople();
						});
					}
					else{//as structure is ready, just add new content inside of target li-tag:
						var newstringArr=newstring.match(pattern2);
						var newHTML=get_string_between(newstringArr[index],'<li>','</li>');
						$("ul#peopleDetails li:eq("+index+")").html(newHTML);
					}
				}
			}
	   }
	 });
}
