//Planetmedia global funciton Version 2.2
//Copyright 2011 all rights reserved
//29-03-2011 last update
dev_="";
var gfxok=dev_+"/gfx/icons/ok.png";
var gfxnonok=dev_+"/gfx/icons/nonok.png";
var gfxempty=dev_+"/gfx/icons/empty.png";
var caret_pos=0;

function showInfo(id)
{
	if($("#infohelp_"+id+"").css("display")=="none")
	{
		$("#infohelp_"+id+"").css("display","block");
		$("#glos_"+id+"").attr("class","GlosTitle_exp");
	}
	else
	{
		$("#infohelp_"+id+"").css("display","none");
		$("#glos_"+id+"").attr("class","GlosTitle");
	}
}

function switch_view(elem)
{
	if($("#"+elem+"").css("display")=="block")
	{
		$("#"+elem+"").css("display","none");
		$("#"+elem+"").css("visibility","hidden");
	}
	else
	{
		$("#"+elem+"").css("display","block");
		$("#"+elem+"").css("visibility","visible");
	}
}
function close_lightbox(elem_array) 
{
	for(i=0;i<elem_array.length;i++)
	{
		$("#"+elem_array[i]+"").css("display","none");	
		$("#"+elem_array[i]+"").css("visibility","hidden");	
	}
}

function open_lightbox(elem_array)
{
	for(i=0;i<elem_array.length;i++)
	{
		$("#"+elem_array[i]+"").css("display","block");	
		$("#"+elem_array[i]+"").css("visibility","visible");	
	}
}

function resize_element(elem_array)
{
	for(i=0;i<elem_array.length;i++)
	{
		$("#"+elem_array[i]+"").css("height",$(document).height());	
	}
	
}
function getThumb()
{
	$("#f1_upload_process").css("visibility","visible");
	$("#f1_upload_process").css("display","block");
}
function stopUpload(thumb,name){
	$("#img_avatar_name").attr("value",name);
	$("#img_avatar").attr("src",thumb);
	$("#f1_upload_process").css("visibility","hidden");
	$("#f1_upload_process").css("display","none");
}

function showTime(element)
{
	var d = new Date();
	hour=d.getHours();
	minutes=d.getMinutes();
	seconds=d.getSeconds();
	if(minutes<10)
		minutes="0"+minutes;
	if(seconds<10)
		seconds="0"+seconds;
	if(hour<10)
		hour="0"+hour;

	$("#"+element+"").html(" "+hour+":"+minutes+":"+seconds+" ");
	//alert(hour+":"+minutes+":"+seconds);
}

function confirmation(message) {
	var answer = confirm(message)
	if (answer){
		return true;
	}
	else{
		return false;
	}
}

//function to print the page content//
function printPage(url,time)
{
	window.focus();
	window.print();
	redirect_timer(url,time)
}

//function to print the page content//
function PrintContent(print_elem,style,end_add)
{
	var DocumentContainer = document.getElementById(print_elem);
	var WindowObject = window.open("", "PrintWindow","width=750,height=650,top=50,left=50,toolbars=no,menubar=no,scrollbars=yes,status=no,resizable=yes");
	WindowObject.document.write("<html><head><link rel='stylesheet'  id='style-css' href='"+style+"' type='text/css' media='screen' /><link rel='stylesheet'  id='style-css' href='"+style+"' type='text/css' media='print' /></head><body style='background:none;'><div>"+DocumentContainer.innerHTML+end_add+"</div></body></html>");
	WindowObject.document.close();
	WindowObject.focus();
	WindowObject.print();
	WindowObject.close();
}

//basic ajax request object//
function ajaxRequest()
{
	 var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
	 if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
	  for (var i=0; i<activexmodes.length; i++){
	   try{
		return new ActiveXObject(activexmodes[i])
	   }
	   catch(e){
		//suppress error
	   }
	  }
	 }
	 else if (window.XMLHttpRequest) // if Mozilla, Safari etc
	  return new XMLHttpRequest()
	 else
	  return false
}

//This function show a message for a certain period, 1st argument is the element (div) that has the message, 2nd argument is the time untill the fade occurs.
function redirect_timer(url,time)
{
	var timePeriodInMs = time;
	setTimeout(function() 
	{ 
		window.location = url;
	},timePeriodInMs);
}

function close_timer(code,time)
{
	var timePeriodInMs = time;
	setTimeout(code,timePeriodInMs);
}
//This function show a message for a certain period, 1st argument is the element (div) that has the message, 2nd argument is the time untill the fade occurs.
function show_message(elem,time)
{
	var timePeriodInMs = time;
	setTimeout(function() 
	{ 
		$("#"+elem+"").css("display","none");
	},timePeriodInMs);
}


//this a threadlike function executes the code usuall a function each specific time
function execute_thread(code,time)
{
	var timePeriodInMs = time;
	return window.setInterval(code,timePeriodInMs);
	//setInterval(code,timePeriodInMs);
}
function stopThread(thread_id)
{
	//alert(thread_id);
	window.clearInterval(thread_id);
}

//check if the first option in select box and returns false if yes, otherwise returns true. Useful when user selection is mandatory for a selection box.
function isSelected(elem_array)
{
	for(i=0;i<elem_array.length;i++)
	{
		if($("#"+elem_array[i]+"").attr("selectedIndex")==0)
			return false;
	}
	return true;
}
//This function clears the fields of a form, usefull for a tabeset, where the form cleaning is desired through the tabs.
function clear_forms(elem_array)
{
	for(i=0;i<elem_array.length;i++)
		$("#"+elem_array[i]+"")[0].reset();
}

function disable_elem(elem_array)
{
	for(i=0;i<elem_array.length;i++)
		$("#"+elem_array[i]+"").attr("disabled",true);
}

// check if all requirements are met (if not empty fields)
function empty_check(elem_array)
{
	var k=0;
	var i=0;
	for(i=0;i<elem_array.length;i++)
	{
		str=$("#"+elem_array[i]+"").attr("value");
		if(str.length>0)
			k=k+1;
	}
	if(k==elem_array.length)
		return true;
	else
		return false;
}
function empty_check_html(elem_array)
{
	var k=0;
	var i=0;
	for(i=0;i<elem_array.length;i++)
	{
		if(elem_array[i].length>0)
			k=k+1;
	}
	if(k==elem_array.length)
		return true;
	else
		return false;
}

function empty_check_sel(elem_array)
{
	var k=0;
	var i=0;
	for(i=0;i<elem_array.length;i++)
	{
		if(document.getElementById(elem_array[i]).length>0)
			k=k+1;
	}
	if(k==elem_array.length)
		return true;
	else
		return false;
}
//This function checks if at least one of the required field/s is/are not empty.
function empty_check_OR(elem_array)
{
	var k=0;
	var i=0;
	for(i=0;i<elem_array.length;i++)
	{
		str=$("#"+elem_array[i]+"").attr("value");
		if(str.length>0)
			k=k+1;
	}
	if(k>0)
		return true;
	else
		return false;
}

function hasChar(elem)
{
	value=$("#"+elem+"").attr("value");
	var k=0;
	for(i=0;i<value.length;i++)
		if(isNaN(value.charAt(i)))
		{
			k=k+1;
		}
	if(k>=1)
		return true;
	else
		return false;
}
// check if an email is in an acceptable form the function works for every key press -->
function check_email(elem)
{
	var mail=false;
	var i=0;
	email=$("#"+elem+"").attr("value");
	for(i=0;i<email.length;i++)
		if(email.charAt(i)=="@")
		{
			i=i+1;
			for(i;i<email.length;i++)
				if(email.charAt(i)==".")
				{
					i=i+1;
					for(i+1;i<email.length;i++)
						if(isNaN(email.charAt(i)))
						{
							i=i+1;
							var k=1;
							var alphaExp = /^[a-zA-Z]+$/;
							for(i;i<email.length;i++)
							{
								if(email.charAt(i).match(alphaExp))
								{
									if(k>=1)
										return true;
									k++;
								}
								else
								{
									$("#"+elem+"").attr("value",email.slice(0,$("#"+elem+"").attr("value").length-1));	
									if(k>=1)
										return true;						
								}
							}
							break;
						}
				}
		}	
}
// check if a website url is in an acceptable form the function works for every key press -->
function check_url(elem)
{
	var mail=false;
	var i=0;
	url=$("#"+elem+"").attr("value");
	for(i=0;i<url.length;i++)
		if(url.charAt(i)==".")
		{
			i=i+1;
			for(i;i<url.length;i++)
				if(url.charAt(i)==".")
				{
					i=i+1;
					for(i+1;i<url.length;i++)
						if(isNaN(url.charAt(i)))
						{
							i=i+1;
							var k=1;
							var alphaExp = /^[a-zA-Z]+$/;
							for(i;i<url.length;i++)
							{
								if(url.charAt(i).match(alphaExp))
								{
									if(k>=1)
										return true;
									k++;
								}
								else
								{
									$("#"+elem+"").attr("value",url.slice(0,$("#"+elem+"").attr("value").length-1));	
									if(k>=1)
										return true;						
								}
							}
							break;
						}
				}
		}	
}

//create keycode table based on exceptions
function keyCodesTable(excepts)
{
	var code_array=new Array();
	for(i=0;i<excepts.length;i++)
	{
		switch (excepts[i])
		{
			case "numbers":
				code_array.push(48,57);
				break;
			case "latin":
				code_array.push(65,90,97,122);
				break;
			case "symbols":
				code_array.push(33,47,58,64,91,96);		
				break;
			case "handling":
				code_array.push(8,9,13);	
				break;
			//case 44:
			//	code_array.push(44,44);	
			//	break;
			case "recps_excepts":
				code_array.push(44,44);	
				break;
		}
	}
	return code_array;
	
}

//determines the keyborad keycode
function findKeyCode(elem,e,excepts) {
	var code;
	if (!e) 
		code = window.event;
	if (e.keyCode) 
		code = e.keyCode;
	else if (e.which) 
		code = e.which;

	ex=0;
	code_array=keyCodesTable(excepts);
	var i=0;
	//alert(code_array.length);
	for(i=0;i<code_array.length;i=i+2)
		for(j=code_array[i];j<=code_array[i+1];j++)
			if(j==code)
				ex=1;
	if(ex==0)
		return false;
}

//checks if the entry is a phone, if yes checks if the 30 suffix appears if not it applies it automatically 
//version 2
function check_phone(elem,start)
{
	inp=$("#"+elem+"").attr("value");
	inp=inp.substring(start,inp.length);
		if(inp.length<=12)
			if((inp.charAt(0)!="3")||(inp.charAt(1)!="0")||(inp.charAt(2)!="6")||(inp.charAt(3)!="9")||(inp.length>12)||(inp.length<10))
			{
				if((inp.charAt(0)=="6")&&(inp.charAt(1)=="9")&&(inp.length==10))
				{
					suffix="30";
					inp=suffix.concat(inp);
					if(start!=0)
					{
						sub=$("#"+elem+"").attr("value");
						sub=sub.substring(0,start);
						inp=sub.concat(inp);
					}
					$("#"+elem+"").attr("value",inp);
					return true;
				}
				else
					return false;				
			}
			else if((inp.charAt(0)=="3")&&(inp.charAt(1)=="0")&&(inp.charAt(2)=="6")&&(inp.charAt(3)=="9")&&(inp.length==12))		
				return true;
			else
				return false;
		else
		{
			$("#"+elem+"").attr("value",$("#"+elem+"").attr("value").slice(0,start).concat(inp.slice(0,inp.length-1)));	
			if((inp.charAt(0)=="3")&&(inp.charAt(1)=="0")&&(inp.charAt(2)=="6")&&(inp.charAt(3)=="9")&&(inp.length>=12))
				return true;
			else
				return false;
		}
		
}

//checks if the input is number,space,enter or (,) character 
function inputdef(inp,excepts)
{
	for(j=0;j<excepts.length;j++)
		if(inp==excepts[j])
			break;

	if(j<excepts.length)
		return inp;
	else
		return 1;
}

//Converts characters to capital
function capitalization(elem) 
{
	$("#"+elem+"").attr("value",$("#"+elem+"").attr("value").toUpperCase());
}

//limits user's input by the limitnum value
function limitText(limitField, limitCount, limitNum,type) {

	if ($("#"+limitField+"").attr("value").length>limitNum) 
		$("#"+limitField+"").attr("value",$("#"+limitField+"").attr("value").substring(0,limitNum));
	else
		if(type=="text")
		{
			if(limitCount!="empty")
			{
				$("#"+limitCount+"").attr("value",limitNum - $("#"+limitField+"").attr("value").length);
			}
		}
		else
		{
			if($("#"+limitCount+"").html()!="")
			{
				$("#"+limitCount+"").html(limitNum - $("#"+limitField+"").attr("value").length);
			}
		}
		
	capitalization(limitField);
		
	if($("#"+limitField+"").attr("value").length>0)
		return true;
	else
		return false;
}

//function that gets the mouse curret position
function getCaret(elem) {
	
  if (document.getElementById(elem).selectionStart) { 
    return caret_pos=document.getElementById(elem).selectionStart; 
  } else if (document.getElementById(elem).selection) { 
    document.getElementById(elem).focus(); 

    var r = document.getElementById(elem).selection.createRange(); 
    if (r == null) { 
      return 0; 
    } 

    var re = document.getElementById(elem).createTextRange(), 
        rc = re.duplicate(); 
    re.moveToBookmark(r.getBookmark()); 
    rc.setEndPoint('EndToStart', re); 
    return caret_pos=rc.text.length; 
  } 
  return 0; 
}

function clear_field(elem_array) {
	for(i=0;i<elem_array.length;i++)
		$("#"+elem_array[i]+"").attr("value","");
}

	
function unselect(elem_array) {
	for(i=0;i<elem_array.length;i++)
		$("#"+elem_array[i]+"").attr("selectedIndex","0");
	return true;
}

//function that checks the two elements of the array if they are equal
function cross_check(elem_array)
{
	var i=0;
	var j=1;
	var n=0;
	
	if(($("#"+elem_array[i]+"").attr("value").length)!=($("#"+elem_array[j]+"").attr("value").length))
		return false;
	else if((($("#"+elem_array[i]+"").attr("value").length)==0)&&(($("#"+elem_array[j]+"").attr("value").length)==0))
		return false;
	else
		if($("#"+elem_array[i]+"").attr("value").length==$("#"+elem_array[j]+"").attr("value").length)
		{
			for(k=0;k<$("#"+elem_array[i]+"").attr("value").length;k++)
			{
				if($("#"+elem_array[i]+"").attr("value").charAt(k)==$("#"+elem_array[j]+"").attr("value").charAt(k))
					n=n+1;
			}
			if(n==$("#"+elem_array[i]+"").attr("value").length)
				return true;
			else
				return false;
		}
		else
			return false;			
}

//one function to rule them all.. this function checks for the whole requierments, 
//first argument is an array with function retunr values, if all are true then the 2nd argumenet (usually a button) enables it self 
function requirements_check(state_array,target_elem_array)
{
	var k=0;
	for(i=0;i<state_array.length;i++)
		if(state_array[i])
			k=k+1;
	if(k==state_array.length)
	{
		for(i=0;i<target_elem_array.length;i++)
			$("#"+target_elem_array[i]+"").attr("disabled",false);
		return true;
	}
	else	
	{
		for(i=0;i<target_elem_array.length;i++)
			$("#"+target_elem_array[i]+"").attr("disabled",true);
		return false
	}
}


function validate_field_icon(bool_val_array,elem)
{
	var k=0;
	for(i=0;i<bool_val_array.length;i++)
		if(bool_val_array[i])
			k=k+1;
	if(k==bool_val_array.length)
	{
		$("#"+elem+"_img").attr("src",gfxok);
		return true;
	}
	else	
	{
		$("#"+elem+"_img").attr("src",gfxnonok);
		return false
	}
}

function restore_field_icon(elem_array)
{
	for(i=0;i<elem_array.length;i++)
		$("#"+elem_array[i]+"_img").attr("src",gfxempty);
}

function displaycity(container,weather_img,location,file_path)
{
	var xmlhttp=new ajaxRequest();
		xmlhttp.onreadystatechange=function(){
		 if (xmlhttp.readyState==4){
		  if (xmlhttp.status==200 || window.location.href.indexOf("http")==-1){
			//document.getElementById(container).innerHTML=xmlhttp.responseText;
			//alert(xmlhttp.responseText);
			weather_parts=xmlhttp.responseText.split("/|/");
			for(i=0;i<container.length;i++)
				$("#"+container[i]+"").html(weather_parts[i]);
			$("#"+weather_img+"").html("<img src='/gfx/weather/"+weather_parts[weather_parts.length-1]+"' height='160px'/>");
			}
			
		  else{
		   alert("An error has occured making the request")
		  }
		 }
		}
		
		var value=encodeURIComponent(document.getElementById(location).value)
		var params = "location="+value;
		xmlhttp.open("POST",file_path,true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
		xmlhttp.send(params)	
}
