//Set Cookie
function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+
	"; path=/";
}

function change_contact_email(val)
{
	if(val == 'Sales')
	{
		$('conatact_email').style.display = 'block';
		$('conatact_email').innerHTML = 'sales@thoughtmedia.com';
		$('cont_email').innerHTML = 'sales@thoughtmedia.com';
		
	}
	else if(val == 'Billing')
	{
		$('conatact_email').style.display = 'block';
		$('conatact_email').innerHTML = 'billing@thoughtmedia.com';
		$('cont_email').innerHTML = 'billing@thoughtmedia.com';
	}
	else if(val == 'Management')
	{
		$('conatact_email').style.display = 'block';
		$('conatact_email').innerHTML = 'adam@thoughtmedia.com';
		$('cont_email').innerHTML = 'adam@thoughtmedia.com';
	}
	else
	{
		$('conatact_email').style.display = 'none';
		$('conatact_email').innerHTML = '';
	}
}

function submit_comment(obj,divObj)
{
	if(obj.full_name.value == '' || obj.full_name.value == 'Your Name')
	{
		obj.full_name.focus();
		alert("Your Name Can not be blank");
		return false;
	}
	if(obj.email.value == '' || obj.email.value == 'Email')
	{
		obj.email.focus();
		alert("Email Can not be blank");
		return false;
	}
	var regEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if(!regEmail.test(obj.email.value))
	{
		obj.email.focus();
		alert("Email should be a valid email");
		return false;
	}
	if(obj.comment.value == '' || obj.comment.value == 'Your Comment')
	{
		obj.comment.focus();
		alert("Comment Can not be blank");
		return false;
	}
	if(obj.security_code.value == '' || obj.security_code.value == 'Enter Code')
	{
		obj.security_code.focus();
		alert("Please enter code.");
		return false;
	}
	
	new Ajax.Updater('', obj.action, {asynchronous:true, method: obj.method, postBody:Form.serialize(obj)+"&rndval="+new Date().getTime(),
		onComplete: function(transport){
			
			if(transport.responseText == '1')
			{
		  		$('request_container_msg').style.display = 'none';
				$(divObj).innerHTML = 'Your comment submitted successfully.';
			}
			else
			{
				$('request_container_msg').style.display = 'block';
				$('request_container_msg').innerHTML = 'Sorry, you have provided an invalid security code.';
			}
	    },	
		evalScripts: true});
	return false;
}

function load_month(year)
{
	$('year_blog_msg').style.display = 'block';
	bust = new Date().getTime();
	//alert(question_number);
	ajxpg('ajax/load_month.php?'+ bust +'&year='+ year,'month_cont');
	
}

function month_blog(val)
{
	window.location.href = val;
}
function submit_request(obj,divObj)
{
	if(obj.full_name.value == '' || obj.full_name.value == 'Full Name')
	{
		obj.full_name.focus();
		alert("Full Name Can not be blank");
		return false;
	}
	if(obj.email.value == '' || obj.email.value == 'Email')
	{
		obj.email.focus();
		alert("Email Can not be blank");
		return false;
	}
	var regEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if(!regEmail.test(obj.email.value))
	{
		obj.email.focus();
		alert("Email should be a valid email");
		return false;
	}
	if(obj.phone.value == '' || obj.phone.value == 'Phone')
	{
		obj.phone.focus();
		alert("Phone Can not be blank");
		return false;
	}
	if(obj.security_code.value == '')
	{
		obj.security_code.focus();
		alert("Please enter code.");
		return false;
	}
	
	new Ajax.Updater(divObj, obj.action, {asynchronous:true, method: obj.method, postBody:Form.serialize(obj)+"&rndval="+new Date().getTime(),
		onComplete: function(transport){
			//alert(transport.responseText);
			if(transport.responseText == '1')
			{
		  		$('request_container_msg1').innerHTML = 'Thank you for your request. We will be in touch shortly.';
				$('request_container_msg1').style.display = 'block';
				$('request_container').style.display = 'none';
			}
			else
			{
				$(divObj).style.display = 'block';
				$(divObj).innerHTML = 'Invalid security code.';
			}
	    },	
		evalScripts: true});
	return false;
}
//Get Cookie
function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
	  	if (c_start!=-1)
		{
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

//change Font Size
var CurrentFontSize = (getCookie('SPECTRA_FONT'))?parseInt(getCookie('SPECTRA_FONT').replace('sfont','')):parseInt('3');
function switchFontSize (ckname,val){
	var bd = document.body;
	switch (val) {
		case 'inc':
			if (CurrentFontSize+1 < 6) {
				CurrentFontSize++;
				bd.className = 'sfont'+CurrentFontSize;
			}		
		break;
		case 'desc':
			if (CurrentFontSize-1 > 0) {
				CurrentFontSize--;
				bd.className = 'sfont'+CurrentFontSize;
			}		
		break;
		default:
			CurrentFontSize = val;
			bd.className = 'sfont'+CurrentFontSize;
	}
	setCookie(ckname, 'sfont'+CurrentFontSize, 30)
	//Cookie.set(ckname, CurrentFontSize,{duration:365});
}

//Global Function to process Ajax request
function GetXmlHttpObject() {
	var request_type;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
	request_type = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
	request_type = new XMLHttpRequest();
	}
	return request_type;
}

//Function to Draw error Message
function alert_box(msg)
{
	alert(msg);
}

//Validate Sponsor
function validate_sponcer(obj)
{
	val = obj.value;
	if(val == "")
	{
		document.getElementById("err_msg").innerHTML = "Please enter your input for the highlighted fields";
		obj.style.backgroundColor = highLightColor;
		obj.style.borderColor = highLightBorder;
		return false;
	}
	else
	{
		document.getElementById("err_msg").innerHTML = "";
	}
	var httpSponce = GetXmlHttpObject();
	url = 'ajax/user_validate.php?query='+val+'&request=sponcer';
	bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
	url = url+bustcacheparameter;
	httpSponce.open("GET", url, true);
	httpSponce.onreadystatechange = function () {
		if (httpSponce.readyState == 4) {
			if(httpSponce.responseText == '0')
			{
				createErrorMsg(obj,'Please contact your sponsoring hospital for assistance,\nif you feel you have received this message in error.');
				obj.style.backgroundColor = highLightColor;
				obj.style.borderColor = highLightBorder;
				obj.focus();
				document.getElementById('registration_form_div').style.display = 'none';
			}
			else
			{
				removeErrorMsg(obj);
				document.getElementById('registration_form_div').style.display = 'block';
				document.getElementById("chk_sponsor").style.display = 'none';
				document.getElementById("sponsor_code").readOnly = "readonly";
				populateHospital(httpSponce.responseText);
				document.getElementById('userid').focus();
			}
		}
	};
	httpSponce.send(null);
}

function populateHospital(id)
{
	var httpHosp = GetXmlHttpObject();
	url = 'ajax/user_validate.php?query='+id+'&request=getHosp';
	bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
	url = url+bustcacheparameter;
	httpHosp.open("GET", url, true);
	httpHosp.onreadystatechange = function () {
		if (httpHosp.readyState == 4) {
			if(httpHosp.responseText != '')
			{
				document.getElementById('divHospitalAffilition').innerHTML = httpHosp.responseText;
			}
		}
	};
	httpHosp.send(null);
}
// Validate UserID Unique
function validate_userid(obj)
{
	val=obj.value;
	var httpUserID = GetXmlHttpObject();
	url = 'ajax/user_validate.php?query='+val+'&request=userid';
	bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
	url = url+bustcacheparameter;
	httpUserID.open("GET", url, true);
	httpUserID.onreadystatechange = function () {
		if (httpUserID.readyState == 4) {
			//alert(httpUserID.responseText);
			if(httpUserID.responseText!='1')
			{
				createErrorMsg(obj,'Your user id is already taken.\n Please select another.');
				obj.style.backgroundColor = highLightColor;
				obj.style.borderColor = highLightBorder;
				obj.focus();
			}
			else
			{
				removeErrorMsg(obj);
				document.getElementById('password').focus();
			}
		}
	};
	httpUserID.send(null);
}


//Change Speciality in case Other Selected
function change_specility(val)
{
	if(val == 'Other')
	{
		document.getElementById('speciality1').style.visibility = '';
	}
	else
	{
		document.getElementById('speciality1').style.visibility = 'hidden';
	}
}

function specify_other(val,obj, name)
{
	if(!document.getElementById(name))	
	obj.parentNode.innerHTML += '<input type="text" id="'+name+'" name="'+name+'" class="R" value="'+ val +'" >';
	else
	document.getElementById(name).style.display = 'block';
}

function remove_specify_other(name)
{
	if(document.getElementById(name))	
	{
		document.getElementById(name).style.display = 'none';
	}
}

// Draw Question
var question_number = -1;
function drawQuestion(div, qNaireID, qNaireSectionID, NP)
{
	if(NP=='PREV')
		question_number--;
	else
		question_number++;

	bust = new Date().getTime();
	//alert(question_number);
	ajxpg('ajax/questions.php?'+ bust +'&qnaire_id='+ qNaireID +'&qnaire_section_id='+ qNaireSectionID +'&qNumber='+question_number,div);
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function create_request_string(theForm)
{
	var reqStr = "";

	for(i=0; i < theForm.elements.length; i++)
	{
		isFormObject = false;
		if (theForm.elements[i].tagName == "INPUT")
		{
			switch (theForm.elements[i].type)
			{
				case "text":
				case "hidden":
				reqStr += theForm.elements[i].name + "=" + theForm.elements[i].value;
				isFormObject = true;
				break;

				case "checkbox":
				if (theForm.elements[i].checked)
				{
					reqStr += theForm.elements[i].name + "=" + theForm.elements[i].value;
				}else{
					reqStr += theForm.elements[i].name + "=";
				}
				isFormObject = true;
				break;

				case "radio":
				if (theForm.elements[i].checked)
				{
					reqStr += theForm.elements[i].name + "=" + theForm.elements[i].value;
					isFormObject = true;
				}
			}
		}
		if (theForm.elements[i].tagName == "SELECT")
		{
			var sel = theForm.elements[i];
			reqStr += sel.name + "=" + sel.options[sel.selectedIndex].value;
			isFormObject = true;
		}
		if ((isFormObject) && ((i+1)!= theForm.elements.length))
		{
			reqStr += "&";
		}
	}
	return reqStr;
} 

function submitQuestion(frmObj, div)
{
	qString = create_request_string(frmObj);
	
	bust = new Date().getTime();
	
	QuestionID = frmObj.qID.value;
	AnswerID = getCheckedValue(frmObj.answer);
	
	if(QuestionID && AnswerID)
	{
		//alert(QuestionID+' '+AnswerID);	
		return ajxpg('ajax/submitAnswer.php?'+ bust +'&'+qString, div);
	}
	return false;
}

function CallPrint(divObj)
{
	var prtContent = document.getElementById(divObj);
	var WinPrint = window.open('','','left=0,top=0,width=1,height=1,t oolbar=0,scrollbars=0,status=0');
	WinPrint.document.write(prtContent.innerHTML);
	WinPrint.document.close();
	WinPrint.focus();
	WinPrint.print();
	WinPrint.close();
	prtContent.innerHTML=strOldOne;
}

function load_our_work(page, catg)
{
	bust = new Date().getTime();
	//alert('ajax/load_ourWork.php?'+ bust +'&page='+ page +'&catg='+ catg);
	page = 'ajax/load_ourWork.php?'+ bust +'&page='+ page +'&catg='+ catg;

	new Ajax.Updater('work_body_left', page, {
	  method: 'get',
	  onComplete: function(transport){
		initLytebox();
	  },
	  postBody:'content=1',
	  evalScripts: true
	  });
	  return false;
	//ajxpg('ajax/load_ourWork.php?'+ bust +'&page='+ page +'&catg='+ catg , 'work_body_left');
}

function load_blog(page, catg, month)
{
	bust = new Date().getTime();
	//alert('ajax/load_blog.php?'+ bust +'&page='+ page +'&catg='+ catg +'&month='+ month);
	ajxpg('ajax/load_blog.php?'+ bust +'&page='+ page +'&catg='+ catg +'&month='+ month , 'our_blog');
}

function open_window(val){
	bust = new Date().getTime();
	//alert("ajax/load_work_web_type.php?id="+val+"&"+bust);
	
	//Dialog.alert({url: "ajax/load_work_web_type.php?id="+val+"&"+bust, options: {method: 'get'}}, { width: 540, className: "alphacube", okLabel: "Close"});
	
	Dialog.alert({url: "ajax/load_work_web_type.php?id="+val+"&"+bust, options: {method: 'get'}}, 
            {className: "alphacube", width:540, height:300, okLabel: "Close"});

}
