var prompt;

function showLayerObject( elem ) 
{  
	var vis = elem.style;  // if the style.display value is blank we try to figure it out here  
	if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)   
		 vis.display = 'block';  
	vis.display = 'block';
}

function showLayer( whichLayer )
{  
	var elem, vis; 
	
	elem = findLayer(whichLayer);

	vis = elem.style;  // if the style.display value is blank we try to figure it out here  
	if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)   
		 vis.display = 'block';  
	vis.display = 'block';
}

function hideLayerObject( elem )
{  
	var vis = elem.style;  // if the style.display value is blank we try to figure it out here  
	if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)   
		 vis.display = 'none';  
	vis.display = 'none';
}

function hideLayer( whichLayer )
{  
	var elem, vis; 
	
	elem = findLayer(whichLayer);

	vis = elem.style;  // if the style.display value is blank we try to figure it out here  
	if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)   
		 vis.display = 'none';  
	vis.display = 'none';
}

function findLayer( whichLayer )
{  var elem, vis;  
	if( document.getElementById ) // this is the way the standards work    
		elem = document.getElementById( whichLayer );  
	else if( document.all ) // this is the way old msie versions work      
		elem = document.all[whichLayer];  
	else if( document.layers ) // this is the way nn4 works    
		elem = document.layers[whichLayer];  
	return elem;
}	

function clearSelect(select) {
    for (var i = 0; i < select.options.length; i++) {
        var option = select.options[i];

        if ((option.value != null) && (option.value != "")) {
            select.options[i] = null;
            i--;
        }
    }
}

function populateRegions(select) {
   var myAjax = new Ajax.Request( "common.htm?action=getRegions", {
   		method: 'post', 
   		parameters: "country=" + select.options[select.options.selectedIndex].value,
   		onComplete: updateRegions
   });
}

function updateRegions (originalRequest) {
	var data = eval(originalRequest.responseText);

	if (data.error) {
		alert(data.error);
	}
	else {
		var regions = document.getElementById("regions");

		while (regions.options.length) {
			regions.options[0] = null;
		}
		
		if (data.length > 0) {
			regions.options[regions.options.length] = new Option("Please select...","");
			
			for (var i = 0; i < data.length; i++) {
				var value = eval(data[i]);
				regions.options[regions.options.length] = new Option(value.name,value.id);
			}
		}
	}
}

function toggleLayer( whichLayer )
{  
	var elem, vis;  
	if( document.getElementById ) // this is the way the standards work    
		elem = document.getElementById( whichLayer );  
	else if( document.all ) // this is the way old msie versions work      
		elem = document.all[whichLayer];  
	else if( document.layers ) // this is the way nn4 works    
		elem = document.layers[whichLayer];  
	
	vis = elem.style;  // if the style.display value is blank we try to figure it out here  
	
	if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)    
		vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';  
		
	vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}

String.prototype.replaceAll = function( 
								strTarget, // The substring you want to replace
								strSubString // The string you want to replace in.
							)
{
	var strText = this;
	var intIndexOfMatch = strText.indexOf( strTarget );
 
	// Keep looping while an instance of the target string
	// still exists in the string.
	while (intIndexOfMatch != -1)
	{
		// Relace out the current instance.
		strText = strText.replace( strTarget, strSubString )
 
		// Get the index of any next matching substring.
		intIndexOfMatch = strText.indexOf( strTarget );
	}
 
	// Return the updated string with ALL the target strings
	// replaced out with the new substring.
	return( strText );
}

function DispPwdStrength(iN,sHL)
{
	if(iN>3){ iN=3;}
	for(var i=0;i<4;i++){ var sHCR="css0162";if(i<=iN){ sHCR=sHL;}if(i>0){ GEId("idSM"+i).className=sHCR;}
	GEId("idSMT"+i).style.display=((i==iN)?"inline":"none");}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function highlightButton(evt, s)
{
  if (document.getElementById)
  {
    if (evt.target)
    {
      if ("INPUT" == evt.target.tagName)
      {
      	evt.target.className = s;
      }
    }
    else
    {

      if ("INPUT" == evt.srcElement.tagName)
      {
      	evt.srcElement.className = s;
      }
    }
  }
}

function getKeyChar(event)
{
	var keynum;
	
	if(window.event) // IE
	{
		keynum = event.keyCode
	}
	else if(event.which) // Netscape/Firefox/Opera
	{
		keynum = event.which
	}
	
	if (! keynum || keynum == 8 || keynum == 37 || keynum == 39 || keynum == 116)
	{
		return null;
	}
	
	//alert("keynum:" + keynum);
	
	return String.fromCharCode(keynum)
}

function isValidUserName(event, field){
	
	var keychar = getKeyChar(event);
	
	if (keychar)
	{
			var current = field.value + keychar;
			
			//alert("'" + current + "'");
		
			var expression = /^[a-zA-Z]{1}[a-zA-Z0-9]*$/;
			if(! current.match(expression))
			{
				return false;
			}
	}
	
	return true;
	
}

function isValidPassword(event, field){
	
	var keychar = getKeyChar(event);
	
	if (keychar)
	{
		
		var current = field.value + keychar;
		
		var expression = /^[a-zA-Z0-9!@#$%]{0,12}$/;
		
		if(! current.match(expression))
		{
			return false;
		}
	}
	
	return true;
}


/**
*   Checks for alphanumeric key codes based on ASCII values.
*       48 to 57 (ASCII) = 0 to 9
*       45 to 90 (ASCII) = A to Z
*       97 to 122 (ASCII) = a to z
*       8 (ASCII) = backspace
*       37 (ASCII) = left arrow
*       39 (ASCII) = right arrow
*       13 (ASCII) = enter
*
*       Delete Key
*       - The ASCII table specifies DEL as 127
*       - Firefox & Netscape map DEL to 46, same as . (dot), which means you cannot block . without blocking DEL as well
*       - IE doesn't consider DEL as a key event
*
*   @params:
*       event - key press event
**/
function isNumeric(event) {
    var allowed = false;

    var keyCode = (event.keyCode ? event.keyCode :
                    (event.charCode ? event.charCode :
                        (event.which ? event.which : 0)));

   //9 is tab in Firefox
    //46 is Delete in Firefox

    if ( ((keyCode >=48) && (keyCode <= 57)) ||
      (keyCode == 8) || (keyCode == 37) ||
      (keyCode == 39) || (keyCode == 13) || (keyCode == 9) || (keyCode == 118)  || (keyCode == 46))
    {
        allowed = true;
    }

	if (! allowed)
	{
		//alert("This field allows only numeric values.");
	}

    return allowed;
}

function alphaNumeric(event) {

    var allowed = false;

    var keyCode = (event.keyCode ? event.keyCode :
                    (event.charCode ? event.charCode :
                        (event.which ? event.which : 0)));

   //9 is tab in Firefox
    //46 is Delete in Firefox

    if ( ((keyCode >=65) && (keyCode <= 90)) ||
    	((keyCode >=97) && (keyCode <= 122)) ||
    	((keyCode >=48) && (keyCode <= 57)) ||
      (keyCode == 8) || (keyCode == 37) ||
      (keyCode == 39) || (keyCode == 13) || (keyCode == 9) || (keyCode == 118)  || (keyCode == 46))
    {
        allowed = true;
    }

	if (! allowed)
	{
		//alert("This field allows only numeric values.");
	}

    return allowed;
}

function onChangeTextArea(obj, maxlength) {
   if (obj.value.length > maxlength) {
      obj.value = obj.value.substring(0, maxlength);
      return false;
   }

   return true;
}

function onKeyUpTextArea(obj, maxlength, event){
    // key codes: 0=Button, 8=Backspace, 37,39=arrow keys
    if((event) && ((event.keyCode == 0) || (event.keyCode == 8) ||
       (event.keyCode>=35 && event.keyCode<=40) || event.keyCode == 46)) {
       return true;
    }

    // Processing actual text keys
    var objLength = obj.value.length;

    if(objLength > maxlength){
        obj.value = obj.value.substring(0, maxlength);
        return false;
    }

    return true;
}




function loaded()
{	
  // do nothing just a place holder, override on each page for page specific load events
}

function isEmpty(inputVal) 
{
	if (inputVal == null || inputVal=="") 
	{
   	return true
	} 
	else 
		{ 
			return false 
		}

}

 



function submitenter(method, event)
{
		var keyCode;
		
	  if (window.event) 
	  {
	  	keyCode = window.event.keyCode;
	  }
	  else if (event) 
  	{
  		keyCode = event.which;
  	}
	  
	  if (keyCode && keyCode == 13)
	  {
	  	  method.call();
	  }
	  
	  return true;
}


function hidePopupForm()
{
	hideLayer('overlay');
}


function checkFieldComplete(field, length, next)
{
  if (field.value.length == length && next)
  {
  	if (document.getElementById(next))
  	{
  		document.getElementById(next).focus(true);
  	}
  }
}



function openform(location, params, method, callback)
{
	showLayer('overlay');
	
	//jQuery('#overlay').css("visibility", "visible");
	popup = document.getElementById("overlay");
	//popup.style.visibility = (popup.style.visibility == "visible") ? "hidden" : "visible";
	//popup.innerHTML = "";
	
	jQuery("html").animate({ scrollTop: 0 }, 100);
	jQuery('.overlay').css('height', '1000px');
	
	new Ajax.Request(location, 
  {
  		method: method,
  		parameters: params,
     	onComplete: function(transport, json) 
     	{     
     		if (transport.responseText.indexOf('loginForm') > 0)
     		{
     			document.location = "/myAccount.htm";
     		}
     		else
     		{
     			popup.innerHTML = transport.responseText;
     			
     			if (callback)
     			{
     				callback.call();
     			}
     		}
			}
  });
  
  
}



function checkEntry(event)
{
	var keynum;
	
	if(window.event) // IE
	{
		keynum = event.keyCode
	}
	else if(event.which) // Netscape/Firefox/Opera
	{
		keynum = event.which
	}
	
	if (keynum == 42 || keynum == 126 || keynum == 63 || keynum == 47)
	{
		return false;
	}
	
	return true;
}


var tags; 

var personId;
var personName;

function tag(id, name)
{
	personId = id;
	
	personName = name;
	
	jQuery(".popup-person").hide();

	jQuery.ajax({
	  url: "tagIt.htm",
	  cache: false,
	  success: function(html){
	  	
	    jQuery.prompt(html,{submit: createTag, buttons: { Ok: true, Cancel: false} });
	    
	    if (document.getElementById("tags"))
	    {
		    tags = new Ext.form.ComboBox({
							transform:"tags",
			  		  typeAhead: true,
		        	triggerAction: 'all',
			        forceSelection:false
			    });		 
			 }   
  	}
	});
}

function tagit()
{
	personId = null;
	
	jQuery.ajax({
	  url: "tagIt.htm",
	  cache: false,
	  success: function(html){
	  	
	    jQuery.prompt(html,{submit: createTag, buttons: { Ok: true, Cancel: false} });
	    
 	    if (document.getElementById("tags"))
	    {
	    	 tags = new Ext.form.ComboBox({
							transform:"tags",
			  		  typeAhead: true,
		        	triggerAction: 'all',
			        forceSelection:false
			    });		    
			  }
  		}
	});
}

var saved = false;

function createTag(v,m)
{
	if (saved)
	{
		saved = false;
		return true;
	}
	else
	{	
		if (tags)
		{
			
			tags.selectText();
			
			if (v)
			{
				
				var saveLink;
				
				if (personId)
				{
					saveLink = "createTag.htm?tag=" + tags.getRawValue() + "&link=" + "javascript:showperson('" + personId + "')" + "&title=" + personName;
				}
				else
				{
					saveLink = "createTag.htm?tag=" + tags.getRawValue() + "&link=" + window.escape(document.location) + "&title=" + document.title;
				}
				 
				jQuery.ajax({
				  url: saveLink,
				  cache: false,
				  success: function(html){
	
				    jQuery("#tagItLayer").text("Tag saved.");
				    saved = true;
				    personId = null;
						personName = null;
				  }  
			  });
			  
			}
			else
				{
					//jQuery(".popup-person").show();
					return true;
				}
			}
		else
			{
				return true;
			}
	}
}


function needLogin()
{
	jQuery.prompt('<div>Please register or login to access this feature.</div>',{buttons: { Ok: true} });
}

function showLinks(id)
{
	
	jQuery.ajax({
		  url: "getLinks.htm?id=" + id,
		  cache: false,
		  success: function(html){
		  	jQuery(".popup-person").hide();
				prompt = jQuery.prompt(html,{submit: showpersonpopup, buttons: { Close: false} });
		  }  
	  });
}

function showpersonpopup()
{
	jQuery(".popup-person").show();
	return true;
}


function showperson(id)
{
  		new Ajax.Request("viewPersonInfo.htm", 
			{
		  		method: "post",
		      	parameters: "id=" + id,
		     	onComplete: function(transport, json) 
		     	{
		     		findLayer("tab-personal").innerHTML=transport.responseText;
		     		jQuery('.popup-person').show();	
		     		
		     		jQuery('.popup-person .tab-content-inner:visible').hide();
						jQuery('ul.tabs-horizontal li[class="current"]').removeClass('current');
						jQuery('a[href=\'#tab-personal\']').parents('li').addClass('current');
						jQuery("#tab-personal").fadeIn();
						jQuery('.popup-person .popup-bottom').css('display', 'none').css('display', 'block');
						
			
		     	}
		 	});

			
			new Ajax.Request("viewPersonWorkInfo.htm", 
			{
		  		method: "post",
		      parameters: "id=" + id,
		     	onComplete: function(transport, json) 
		     	{
		     		findLayer("tab-work").innerHTML=transport.responseText;
		     	}
		 	});
		 	
		 	new Ajax.Request("viewPersonMedicalInfo.htm", 
			{
		  		method: "post",
		      parameters: "id=" + id,
		     	onComplete: function(transport, json) 
		     	{
		     		findLayer("tab-medical").innerHTML=transport.responseText;
		     	}
		 	});
		 	
		 	
		 	new Ajax.Request("viewPersonSocialInfo.htm", 
			{
		  		method: "post",
		      	parameters: "id=" + id,
		     	onComplete: function(transport, json) 
		     	{
		     		findLayer("tab-social").innerHTML=transport.responseText;
		     	}
		 	});
		 	
		 	new Ajax.Request("viewPersonPictures.htm", 
			{
		  		method: "post",
		      parameters: "id=" + id,
		     	onComplete: function(transport, json) 
		     	{
		     		findLayer("pictureList").innerHTML=transport.responseText;
		     	}
		 	});
		 	
		 	new Ajax.Request("viewPersonTags.htm", 
			{
		  		method: "post",
		      parameters: "id=" + id,
		     	onComplete: function(transport, json) 
		     	{
		     		findLayer("tagList").innerHTML=transport.responseText;
		     	}
		 	});
	
	
	jQuery('ul.tabs-horizontal a').click(function()
		{
			if (jQuery(this).parents('li').hasClass('current')) return false;
			jQuery('.popup-person .tab-content-inner:visible').hide();
			jQuery('ul.tabs-horizontal li[class="current"]').removeClass('current');
			jQuery(this).parents('li').addClass('current');
			jQuery(jQuery(this).attr('href')).fadeIn();
			jQuery('.popup-person .popup-bottom').css('display', 'none').css('display', 'block');
			return false;
		});
		
		//popup pictures
		jQuery('#tab-pictures p.buttons a').click(picturesToggle);
		function picturesToggle ()
		{
			if (jQuery(this).hasClass('next'))
			{
				if (jQuery('#tab-pictures ul.pictures li:last').hasClass('current'))
					var next = jQuery('#tab-pictures ul.pictures li:first');
				else
					var next = jQuery('#tab-pictures ul.pictures li.current').next();
			}
			else
			{
				if (jQuery('#tab-pictures ul.pictures li:first').hasClass('current'))
					var next = jQuery('#tab-pictures ul.pictures li:last');
				else
					var next = jQuery('#tab-pictures ul.pictures li.current').prev();
			}
			jQuery('#tab-pictures ul.pictures li.current').hide();
			jQuery('#tab-pictures ul.pictures li.current').removeClass('current');
 			next.fadeIn(); //add animation
 			next.addClass('current');
 			jQuery('.popup-person .popup-bottom').css('display', 'none').css('display', 'block'); //bugs in IE
			return false;
		}
		
		jQuery('.popup-person .buttons input').click(function(){jQuery('.popup-person').hide();return false;});	
		
		jQuery(".popup-person").draggable({'cursor':'move'});
}



function hideall()
{
	//jQuery(".popup-person").hide();
	
	if (prompt)
	{
		 prompt.hide();
	}
	
}


function setupAlert(target, type)
{
	jQuery.getJSON('getSSO.htm', function(json)
	{
		if (json[0].sso == 'none')
		{
			alert("Please login or register to access this feature.");
		}
		else
		{
			jQuery(".popup-person").hide();
			
			jQuery.get("/alertster.htm?target=" + target + "&type=" + type, function(data)
			{
				jQuery.prompt(data,{buttons: { Close: true}, prefix: 'alert', callback: closeAlertster});
			});
			
		}
		
	});
	
	return false;
}

function closeAlertster()
{
	jQuery(".popup-person").show();
}


function alertster()
{
	jQuery.getJSON('getSSO.htm', function(json)
	{
		if (json[0].sso == 'none')
		{
			alert("Please login or register to access this feature.");
		}
		else
		{
			jQuery(".popup-person").hide();
			
			document.location = "/messaging.htm?sso=" + json[0].sso;
			/*
			jQuery.get("/messaging.htm?sso=" + json[0].sso, function(data)
			{
				 document.body.innerHTML = data;
			});
			*/
			
		}
		
	});
	return false;
}




function updateProfile()
{
	
	
	var options = {
	 	success:processUpdateProfileResponse,
	 	error:processUpdateProfileResponse
	 }; 
	
	
	jQuery(".register-form").ajaxForm(options); 
	 
	jQuery(".register-form").submit();
}

function processUpdateProfileResponse(data)
{
	alert("got response back 123" + data);
}

function scheduleAlertsterMessage()
{
	//var obj = jQuery("body");
	tb_show('Schedule message', 'schedule.htm?keepThis=false&TB_iframe=true&height=550&width=700', true);
	//jQuery("top.body").blur();
	return false;
	
}


function tb_remove() {
	
 	document.location.reload();
}



function removeProfilePopup()
{
	alert("remove popup is called");
	jQuery("#TB_imageOff").unbind("click");
	jQuery("#TB_closeWindowButton").unbind("click");
	jQuery("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	jQuery("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		jQuery("body","html").css({height: "auto", width: "auto"});
		jQuery("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	
	return false;
}