function trim(str)
{
    if(!str || typeof str != 'string'){
    	str="";
    	return str;
    }
    return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
}
function isNum(fld){
	return checkfld(fld,"0123456789.");
}
function checkfld(fld,str1){
	str=new String(str1);
	if (fld.value.length==0) return true;
	for (var i=0;i<fld.value.length;i++)
	{
		found=false;
		for (var j=0;j<str.length;j++)
			if((fld.value.substring(i,i+1))==(str.substring(j,j+1)))
			{
				found=true;
				break;
	        }
		if (!found)
		{
			return false;
		}
    }
	return true;
}

function trim1(str)
{
    if(!str || typeof str != 'string'){
    	str="";
    	return str;
    }
    return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'');
}
function isNum1(fld){
	return checkfld(fld,"0123456789- ");
}

function isEmail(obj)
{	
	str = obj.value;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (!filter.test(str))
	{
		alert("Please enter a valid E-mail Id");
		obj.focus();
		return false;
	}
	return true;
}
function isNumber(val, msg)
	{
		if(val==""){
			alert('Please enter '+msg);
			return false;
		}
		if(isNaN(val))
		{
			alert('Please enter '+msg +' in numbers');
			return false;
		}
	return true;
	}

function getDepartmentDataNav(url)
{	
	var prodRow = $("#product_list");
	prodRow.html("<div style='float:left;color:#952E6E;font-weight:bold; height:300px; padding-top:150px; font-size:1.4em;text-align:center;'>Refreshing the list of products which meet your criteria.</span><div style='float:left;'><img src='"+sitePath+"sitecontent/images/ajaxupdator.gif' style='margin-left:350px; margin-top:25px;'/>");

	//document.deptForm.selectedItems.value=document.deptForm.selectedItems.value;
	//alert("pagenation::"+document.deptForm.selectedItems.value);
	
	//alert("department"+url);
	$.post(url, { fromAjax:'Y' },
			  function(data){	
					//alert(data);									
					document.getElementById("departmentData").innerHTML = data;					
					
			  }, "html");

}

function getDepartmentData(url,range)
{	
	var prodRow = $("#product_list");
	prodRow.html("<div style='float:left;color:#952E6E;font-weight:bold; height:300px; padding-top:150px; font-size:1.4em;text-align:center;'>Refreshing the list of products which meet your criteria.</span><div style='float:left;'><img src='"+sitePath+"sitecontent/images/ajaxupdator.gif' style='margin-left:350px; margin-top:25px;'/>");
	var url1 = url.replace("/sort/priceh2l", "").replace("/sort/rating", "").replace("/sort/price", "").replace("/sort/rnd", "");
	var mytool_array=url1.split("/");
	var part_num=0;
	var html = "";
	html = html+ "<style>";
	html = html+".refersh{float:left; color:#953735;  width:554px; font-size:13px; padding-top:30px; text-align:center; font-family:'Lucida Grande','Lucida Sans Unicode',Arial,Verdana,sans-serif;}";
	html = html+".refersh p{margin-top:15px;}";
	html = html+".refersh p.para{margin-bottom:15px;}";
	html = html+"ul.rfrsh{margin:0px; padding:0px; margin-bottom:15px; width:145px; list-style-type:none; margin-left:auto; margin-right:auto; border:1px solid #d99694; color:#000}";
	html = html+"ul.rfrsh li{text-align:left; font-size:12px; font-weight:normal;  text-align:center; line-height:18px;}";
	html = html+"ul.rfrsh li.first{margin:0px; padding:0px; background-color:#e6b9b8; color:#000000; font-size:11px; line-height:24px; text-align:center;}";
	html = html+ "</style>";
	if(mytool_array[4].indexOf("?")>0)
	{
		html=html+"<div class='refersh'><p align='center'><img src='"+sitePath+"sitecontent/images/ajaxupdator.gif'/></p><p class='para'>Refreshing the list of "+mytool_array[4].substring(0,mytool_array[4].indexOf("?"))+"<br />to match your criteria.</p>";
	}else
	{
		html=html+"<div class='refersh'><p align='center'><img src='"+sitePath+"sitecontent/images/ajaxupdator.gif'/></p><p class='para'>Refreshing the list of "+mytool_array[4]+"<br />to match your criteria.</p>";
	}
	var flag=0;
	var rCount=0;
	var cat = "";
	while (part_num < mytool_array.length)
	 {
		if (range.indexOf(mytool_array[part_num]) >0)
		{
			if(html.indexOf(mytool_array[part_num])<0)
			{
				if(rCount>0)
				{
					html=html+"</ul>";
					
				}
				html=html+"<ul class='rfrsh'>";
				html =html+"<li  class='first'>"+mytool_array[part_num].replace("h2l","").replace("-plus-","+").replace("-"," ")+"</li>";
				cat = mytool_array[part_num];
				rCount++;
				flag=1;
			}
		}else if(flag==1)
		{
			var attVal_array=mytool_array[part_num].split("+");
			var index=0;
			while (index < attVal_array.length)
			 {
			    if(cat == "Price"){
			       var attributeValue = ""
			       if(attVal_array[index].indexOf("=") > -1){
			           attributeValue = attVal_array[index].replace("=","< &#163;");
			       }
			       if(attVal_array[index].indexOf("_") > -1){
			           attributeValue = attVal_array[index].replace("_","> &#163;");
			       }
			       if(attVal_array[index].indexOf("-") > -1){
			          attributeValue = "&#163;"+attVal_array[index].replace("-"," - &#163;");
			       }
			       if(attributeValue.indexOf("?")<0)
		    		{
			    	   html =html+"<li>"+attributeValue+"</li>";
		    		}else
		    		{
		    			if(attVal_array[index].indexOf("=") > -1){
					           attributeValue = attVal_array[index].replace("=","< &#163;");
					       }
		    			
		    		   html =html+"<li>"+attributeValue.substring(0,attributeValue.indexOf("?")).replace("_","&").replace("-"," ")+"</li>";
		    		}
			       
			      
			    }else{
			    	if(attVal_array[index]!="sort" || attVal_array[index]!="h2l")
			    	{
			    		if(attVal_array[index].indexOf("?")<0)
			    		{
			    			html =html+"<li>"+attVal_array[index].replace(/-/g," ").replace("_","&").replace("-"," ").replace("-"," ").replace("-"," ").replace("-"," ").replace("-"," ").replace("-"," ")+"</li>";
			    		}else
			    		{
			    			html =html+"<li>"+attVal_array[index].replace(/-/g," ").replace("_","&").replace("-"," ").replace("-"," ").replace("-"," ").replace("-"," ").replace("-"," ").replace("-"," ").substring(0,attVal_array[index].indexOf("?"))+"</li>";
			    		}
			    		
			    	}
			    }
				index++;
			 }
			
		}
	  part_num+=1;
	  }
	html=  html+"</ul><p>Click on any of the links on the left<br />to change your criteria</p>";
	var filterHeight1 = document.getElementById('search-criteria1').clientHeight;
	//alert(filterHeight1);
	if(filterHeight1+50>300)
	{
		var value = filterHeight1 - 250;
		//alert(value);
		html = html + "<div class='refersh' style='height:"+value+"px;'></div>";
	}
	html = html+"</div>";
	prodRow.html(html);
	
	//document.deptForm.selectedItems.value=document.deptForm.selectedItems.value;
	//alert("pagenation::"+document.deptForm.selectedItems.value);
	
	$.post(url, { fromAjax:'Y' },
			  function(data){	
					//alert(data);	
		document.getElementById("departmentData").innerHTML = data;	
		controlDropColums();
					
			  }, "html");
}



function displayProducts(url,searchTerm){
	//alert("hiProduct");
	$.post(url,
			{	
				searchTerm:searchTerm
			},
			function(data)
			{	//alert(data);
				$("#productdata").fadeIn("slow");
				$("#productdata").html(data);					
			},
	"html" );
}
function displayReviews(url,searchTerm){
	//alert("hi");
	$.post(url,
			{	
				searchTerm:searchTerm
			},
			function(data)
			{
				$("#reviewdata").fadeIn("slow");
				$("#reviewdata").html(data);					
			},
	"html" );
}
function displayHelpTopics(url,searchTerm){
	//alert("hihelp");
	$.post(url,
			{	
				searchTerm:searchTerm
			},
			function(data)
			{	//alert(data);
				$("#helpdata").fadeIn("slow");
				$("#helpdata").html(data);					
			},
	"html" );
}

function controlDropColums()
{
	var leftportletheight = document.getElementById('lftdrop').clientHeight;
	var rightportletheight = document.getElementById('rightdrop').clientHeight;
	var productContentheight = document.getElementById('product_list').clientHeight;
	var filterHeight = document.getElementById('search-criteria1').clientHeight;
	var productlistspace = document.getElementById('productlistspace');

	//alert($('#lftdrop').attr('clientHeight'));
	//alert("leftportletheight "+leftportletheight);
	//alert("rightportletheight "+rightportletheight);
	//alert("productContentheight "+productContentheight);
	//alert("filterHeight "+filterHeight);
	
	
	if(filterHeight>productContentheight)
	{
		var diff = filterHeight-productContentheight;
		var value =(diff)+"px";
		productlistspace.style.height = value;
	}
	productContentheight = document.getElementById('product_list').clientHeight;
	//right drop col
	if(productContentheight<rightportletheight)
	{
		//alert("right hide needed");
		var rgtdivs = document.getElementById("rgtdropcoldiv1");
		var rgtdivheight=0;
		//for(var i = 30; i > 1; i--){
		
		for(var i = 1; i <30 ; i++){
			var divid = "rgtdropcoldiv"+i;
			var rgtdivs = document.getElementById(divid);
			if(i==1)
			{
				rgtdivheight = rgtdivs.clientHeight;
			}
			if(rgtdivs != null)
			{
				var portletHeight = rgtdivs.clientHeight;
				if(portletHeight==0)
				{
					portletHeight =250;
				}
				rgtdivheight = rgtdivheight+ portletHeight;
				
				//alert(divid + " productContentheight "+productContentheight + " rgtdivheight "+rgtdivheight + " :: "+rgtdivs.clientHeight);
				rightportletheight = document.getElementById('rightdrop').clientHeight;
				
				//if(productContentheight < rightportletheight)
				if((productContentheight+90) < rgtdivheight)
				{
					rgtdivheight = rgtdivheight-portletHeight;
					rgtdivs.style.display = "none";
				}
			}
		}
	}
	
	//left drop col
	var lh = productContentheight;
	/*if(productContentheight<rgtdivheight)
	{
		lh = rgtdivheight;
	}*/
	if(lh<leftportletheight+filterHeight)
	{
		//alert("hide needed");
		var divs = document.getElementById("lftdropcoldiv1");
		var leftdivheight=0;
		for(var i = 1; i < 5; i++){
			var divid = "lftdropcoldiv"+i;
			var divs = document.getElementById(divid);
			
			if(divs != null)
			{
				leftdivheight =leftdivheight+ divs.clientHeight;
				//alert(divs.clientHeight + " :: "+ i);
				if(lh<filterHeight+leftdivheight)
				{
					//divs.style.visibility = "hidden";
					//divs.style.height = 0;
					divs.style.display = "none";
				}
			}
		}
	}
}
function removeThickBoxEvents() 
{
       $('.thickbox').each(function(i) {
           $(this).unbind('click');
       });
}

function bindThickBoxEvents() 
{	
    removeThickBoxEvents();
    tb_init('a.thickbox, area.thickbox, input.thickbox');
}

function clear_form_elements(ele) {

	alert("ele "+ele);
    $(ele).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });

}
function checkEmail(myForm) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.emailAddr.value)){
return (true)
}
alert("Invalid E-mail Address! Please re-enter.")
return (false)
}

function ClearValue(frmName) {

var frmName=frmName;
var myContainer = document.getElementById(frmName);

var options = myContainer.getElementsByTagName('input');

for (i = 0; i < options.length; i++) {

var opt = options[i];

if (opt.type == 'text') {

opt.value = "";

}

else if (opt.type == 'checkbox' || opt.type == 'radio') {

if (opt.checked) {

opt.checked = false;


}
}

}

var options = myContainer.getElementsByTagName('textarea');

for (i = 0; i < options.length; i++) {

var opt = options[i];

opt.value = "";

}

var options = myContainer.getElementsByTagName('select');

for (i = 0; i < options.length; i++) {

var opt = options[i];

opt.selectedIndex = -1;

}

}

function checkRadio (frmName, rbGroupName) { 
	 var radios = document.forms[frmName].elements[rbGroupName]; 
	 for (var i=0; i <radios.length; i++) { 
	  if (radios[i].checked) { 
	   return true; 
	  } 
	 } 
	 return false; 
	}

//***************************************************************************/
/*V7 common javascript code 
 */
var count = 0;
var pathname = window.location.href;
var sepPath = pathname.split("/");
var filePath = "";
for (i=0;i< parseInt(sepPath.length)-1; i++ )
{
	filePath = filePath + sepPath[i] + "/";
}
$(function(){
	var w_mid = $(".mid").width();
	var w_lst = $("#lst > table").width();
	var w_li = $("#lst > table tr td:first").width();
	//w_li = w_li * 4; //Number of images to move

	w_li3 = w_li * 3;
	w_li4 = w_li * 4;

	$(".last4 > a").click(function (){
		MoveFront(( w_lst - w_li4 ),w_li4);
	});
	$(".first4 > a").mousedown(function (){
		MoveBack( w_li4);
	});

	$(".last3 > a").click(function (){
		MoveFront(( w_lst - w_li3 ),w_li3);
	});
	$(".first3 > a").mousedown(function (){
		MoveBack( w_li3);
	});
})
function MoveFront(t,w)
{
	count = count + w;
	if (count <= t)
	{
		$("#lst > table").animate( { marginLeft:"-"+count+"px"}, 1000 );
		var tar = $(".mid").prev().attr("class");
		$("."+tar+" > a").css({'backgroundImage': 'url('+ sitePath + 'sitecontent/images/gallery_left.gif)','backgroundRepeat': 'no-repeat','backgroundPosition': 'center center','cursor': 'pointer'});
	}
	else
	{
		count = t;
		$("#lst >table").animate( { marginLeft:"-"+count+"px"}, 1000 );
		$(".mid + div > a").css({'backgroundImage': 'url('+ sitePath + 'sitecontent/images/gallery_right_normal.gif)','backgroundRepeat': 'no-repeat','backgroundPosition': 'center center','cursor': 'default'});
		
	}
}
function MoveBack(w)
{
	count = count - w;
	if (count >= 1)
	{
		$("#lst >table").animate( { marginLeft:"-"+count+"px"}, 1000 );
		$(".mid + div > a").css({'backgroundImage': 'url('+ sitePath + 'sitecontent/images/gallery_right.gif)','backgroundRepeat': 'no-repeat','backgroundPosition': 'center center','cursor': 'pointer'});
	}
	else
	{
		count = 0;
		$("#lst > table").animate( { marginLeft:"-"+count+"px"}, 1000 );
		//$(".mid - div > a").css({'backgroundImage': 'url('+ filePath + 'images/gallery_left_normal.gif)','backgroundRepeat': 'no-repeat','backgroundPosition': 'center center'});
		var tar = $(".mid").prev().attr("class");
		$("."+tar+" > a").css({'backgroundImage': 'url('+ sitePath + 'sitecontent/images/gallery_left_normal.gif)','backgroundRepeat': 'no-repeat','backgroundPosition': 'center center','cursor': 'default'});

	}
}



$(document).ready(function(){
	$('.tab').click(function () {
		$('#tabsF > .tabs > li.active').removeClass('active');
		$(this).parent().addClass('active');
		$('.tab_contents_container > div.tab_contents_active').removeClass('tab_contents_active');
		$(this.rel).addClass('tab_contents_active');
	});

	$("#list1 > li > a").click(function(){
		var indx = $("#list1 > li > a").index(this);
		$("#list1 > li > a").removeClass("active");
		$("#list1 > li > a").eq(indx).addClass("active");
		$("#list1 > li > .cont").slideUp("slow");
		$("#list1 > li > .cont").eq(indx).slideDown("slow");
		
	});	

	$("#click").toggle(
			  function () {
				$("#home-id").slideUp(500,function(){
				$("#click").html("Open [<b>+</b>]");
					if(typeof(displayHeader) != "undefined")
					{						
						$.cookie(displayHeader,"close", { expires: 7 });
					}
				});
				
			  },
			  function () {
				$("#home-id").slideDown(500,function(){
					$("#click").html("Close [<b>x</b>]");
					if(typeof(displayHeader) != "undefined")
					{						
						$.cookie(displayHeader,"open", { expires: 7 });
					}
					
				});
				
			  });
	
	if(typeof(displayHeader) != "undefined")
	{
		displayHeaderCookieVal = $.cookie(displayHeader);
		
		if(displayHeaderCookieVal == "close"){
			
			$("#click").click()				
		}
	}

});


$(function (){
	
	var indx=-1;
	var msg="";
	// Tool tips - message
	$(".tipmsg").hover(function(e) {
		indx = $(".tipmsg").index(this);
		msg = $(".tipmsg").eq(indx).attr("rel");
		$('.tips > p').text("");
		$('.tips > p').append(msg);
		$('.tips').css({top:e.pageY + 10, left:e.pageX });
		$('.tips').show();
	}, function() {
		$('.tips').hide();
	});
	$(".tipmsg").mousemove(function(e) {
		$('.tips').css({top:e.pageY + 10, left:e.pageX });
	});
	// End of Tool tips - message


	// Tool tips - Image
	$(".TipsImg").hover(function(e) {
		indx = $(".TipsImg").index(this);
		msg = $(".TipsImg").eq(indx).attr("rel");
		$('.tipsImg').text("");
		$('.tipsImg').append('<img src="'+msg+'" alt="" />');
		$('.tipsImg').css({top:e.pageY - 0, left:e.pageX + 18});
		$('.tipsImg').show();
	}, function() {
		$('.tipsImg').hide();
	});
	$(".TipsImg").mousemove(function(e) {
		$('.tipsImg').css({top:e.pageY - 0, left:e.pageX + 18});
	});
	// End of - Tool tips - Image
})

// Book marks
function showBookmarks()
{
	var obj = $(".bookmark");
	var offset = obj.offset();
	$('#bookmarks').css({top:(offset.top+216)});
	$('#bookmarks').show();
}
function hideBookmarks()
{
	$('#bookmarks').hide();
}


function socialBookmark(str) {
	if (str == 'Delicious') {
		var URL = "http://del.icio.us/post?url=" + window.location.href + "&title=" + document.title;
		window.open(URL, str, 'toolbar=no,width=800,height=450,resizable=yes,scrollbars=yes');
	}
	if (str == 'Digg') {
		var URL = "http://digg.com/submit?phase=2&url=" + window.location.href + "&title=" + +encodeURIComponent(document.title);
		window.open(URL, str, 'toolbar=no,width=800,height=450,resizable=yes,scrollbars=yes');
		var URL = "http://digg.com/remote-submit?phase=2&url=" + window.location.href + "&title=" + document.title;
	}
	if (str == 'reddit') {
		var URL = "http://reddit.com/submit?url=" + window.location.href + "&title=" + document.title;
		window.open(URL, str, 'toolbar=no,width=800,height=450,resizable=yes,scrollbars=yes');
	}
	if (str == 'Facebook') {
		var URL = "http://www.facebook.com/sharer.php?u=" + window.location.href;
		window.open(URL, str, 'toolbar=no,width=800,height=450,resizable=yes,scrollbars=yes');
	}
	if (str == 'StumbleUpon') {
		var URL = "http://www.stumbleupon.com/submit?url=" + window.location.href + "&title=" + document.title;
		window.open(URL, str, 'toolbar=no,width=800,height=450,resizable=yes,scrollbars=yes');
	}
	return false;
}

function createBookmarkLink()
{
	var title = "Graduate Record Examination"; var url = window.location.href;
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
	window.external.AddFavorite( url, title);
	}else if(window.opera && window.print) { // Opera Hotlist
	return true;
	}
}
// End of Book marks

function roll_over(img_name, img_src)
{
document[img_name].src = img_src;
}

function captchaReload()
{
	var timeStmp = new Date().getTime();
	jQuery("#captcha").attr("src", sitePath+"/secure/captcha.htm?"+timeStmp)
}
