/*intranet, timesheet, pto Javascript */
function validate()
{
	if(document.getElementById('allDay1').checked==true)
	{
		return true;
	}
	else
	{
		if(document.getElementById('fromAmPm').value==""||document.getElementById('toAmPm').value=="")
		{
			alert("Please select AM/PM option");
			return false;
		}	
		if(document.getElementById('fromHour').value=="HH" || document.getElementById('fromMinute').value=="MM")
		{
			alert("Hour or Minute field can not be empty for from time.");
			return false;
		}
		if(document.getElementById('toHour').value=="HH" || document.getElementById('toMinute').value=="MM")
		{
			alert("Hour or Minute field can not be empty for to time.");
			return false;
		}
		var fromH = Number(document.getElementById('fromHour').value);
		var fromM = Number(document.getElementById('fromMinute').value);
		var toH = Number(document.getElementById('toHour').value);
		var toM = Number(document.getElementById('toMinute').value);
		
		
		if(fromH > 12 || fromH < 0)
		{
			alert("Hour value is not appropriate for from time.");
			return false;
		}
		if(fromM > 60 || fromM < 0)
		{
			alert("Minute value is not appropriate for from time.");
			return false;
		}
		if(toM > 60 || toM < 0)
		{
			alert("Minute value is not appropriate for to time.");
			return false;
		}
		if(toH > 12 || toH < 0)
		{
			alert("Hour value is not appropriate for to time.");
			return false;
		}
	}	
}

/* Blank function declaration to avoid JavaScript error in Browsers*/
function timer(){}
function caldays(){}
function displayMsgLeave(){}
function calTotalDays(){}
function displayName(){}
function checkDesc(){}
function calChars(){}
function ajax_approve(){}
/* END:Blank function declaration to avoid JavaScript error in Browsers*/
/* END: intranet, timesheet, pto Javascript */


/* iiSupport and Project management Javascript */
function setHiddenValue2(selectBox) 
{			
	var units = document.getElementById(selectBox).options[document.getElementById(selectBox).selectedIndex].text;
	if(units == 'Other')
	{
		document.getElementById('otherApp').value = '';
		document.getElementById('showOthers').style.display = 'inline';
	}
	else
	{
		document.getElementById('showOthers').style.display = 'none';
	}
}

function setHiddenValue1(selectBox)
{
	var units = document.getElementById(selectBox).options[document.getElementById(selectBox).selectedIndex].value;
	if(units == '1')
	{
		document.getElementById('applicationIT').value = '';
		document.getElementById('showITApplications').style.display = 'inline';
		document.getElementById('showPriority').style.display = 'inline';
		document.getElementById('showPriority1').style.display = 'inline';
		document.getElementById('showPriority2').style.display = 'inline';
	}
	else
	{
		document.getElementById('showITApplications').style.display = 'none';
	}
	if(units == '2')
	{
		document.getElementById('applicationSupport').value = '';
		document.getElementById('showSupportApplications').style.display = 'inline';
		document.getElementById('showPriority').style.display = 'inline';
		document.getElementById('showPriority1').style.display = 'inline';
		document.getElementById('showPriority2').style.display = 'inline';
	}
	else
	{
		document.getElementById('showSupportApplications').style.display = 'none';
	}
	if(units == '3')
	{
		document.getElementById('applicationSuggestions').value = '';
		document.getElementById('showSuggestionsApplications').style.display = 'inline';
		document.getElementById('showPriority').style.display = 'inline';
		document.getElementById('showPriority1').style.display = 'inline';
		document.getElementById('showPriority2').style.display = 'inline';
	}
	else
	{
		document.getElementById('showSuggestionsApplications').style.display = 'none';
	}
	if(units == '4')
	{
		document.getElementById('applicationContent').value = '';
		document.getElementById('showContentApplications').style.display = 'inline';
		document.getElementById('showPriority').style.display = 'inline';
		document.getElementById('showPriority1').style.display = 'inline';
		document.getElementById('showPriority2').style.display = 'inline';
	}
	else
	{
		document.getElementById('showContentApplications').style.display = 'none';
	}
	if(units == 'Project')
	{
		document.getElementById('showPriority').style.display = 'none';
		document.getElementById('showPriority1').style.display = 'none';
		document.getElementById('showPriority2').style.display = 'none';
	}
	else
	{
		document.getElementById('showPriority').style.display = 'inline';
	}
	if(units == 'addToExistingProject')
	{
		document.getElementById('showExistingProjects').style.display = 'inline';
		document.getElementById('showPriority').style.display = 'inline';
		document.getElementById('showPriority1').style.display = 'inline';
		document.getElementById('showPriority2').style.display = 'inline';
	}
	else
	{
		document.getElementById('showExistingProjects').style.display = 'none';
	}
	
	if(units == '6' || units == '7' || units == '8' || units == '9' || units == '10')
	{
		document.getElementById('showOthers').style.display = 'inline';
	}
	document.getElementById('applicationIT').value = '';
	document.getElementById('applicationSupport').value = '';
	document.getElementById('applicationSuggestions').value = '';
	document.getElementById('applicationContent').value = '';
	document.getElementById('applicationProjects').value = '';
	document.getElementById('otherApp').value = '';
	if(!(units == '6' || units == '7' || units == '8' || units == '9' || units == '10'))
		document.getElementById('showOthers').style.display = 'none';
}

function validateRequestForm() { 
	var errmsg = "";
	if(document.etaForm.ETA.value == "")
	{
		errmsg = errmsg + "Please select an ETA Date from the calender.\n";
	}
	if(document.getElementById('assignTo').value == "")
	{
		errmsg = errmsg + "Please select a user to which request will be assigned.\n";
	}
	var errlen = errmsg.length;
	if(errmsg.length!=0)
	{
		alert(errmsg);
		return false;
	}
	else
	{
		return true;
	}
}

function validateUserForm() { 
	var errmsg = "";
	if(document.getElementById('problem').value == "")
	{
		errmsg = errmsg + "Please Enter a problem.\n";
	}
	if(document.getElementById('department').value == "")
	{
		errmsg = errmsg + "Please select a department.\n";
	}
	if((document.getElementById('department').value == "1") && (document.getElementById('applicationIT').value == ""))
	{
		errmsg = errmsg + "Please select an IT application.\n";
	}
	if((document.getElementById('department').value == "2") && (document.getElementById('applicationSupport').value == ""))
	{
		errmsg = errmsg + "Please select a support application.\n";
	}
	if((document.getElementById('department').value == "3") && (document.getElementById('applicationSuggestions').value == ""))
	{
		errmsg = errmsg + "Please select a suggestion application.\n";
	}
	if((document.getElementById('department').value == "4") && (document.getElementById('applicationContent').value == ""))
	{
		errmsg = errmsg + "Please select a content application.\n";
	}
	if((document.getElementById('applicationIT').value == "Other") && (document.getElementById('otherApp').value == ""))
	{
		errmsg = errmsg + "Please enter the other IT application's name.\n";
	}
	if((document.getElementById('applicationSupport').value == "Other") && (document.getElementById('otherApp').value == ""))
	{
		errmsg = errmsg + "Please enter the other support application's name.\n";
	}
	if((document.getElementById('applicationSuggestions').value == "Other") && (document.getElementById('otherApp').value == ""))
	{
		errmsg = errmsg + "Please enter the other suggestion application's name.\n";
	}
	if((document.getElementById('applicationContent').value == "Other") && (document.getElementById('otherApp').value == ""))
	{
		errmsg = errmsg + "Please enter the other content application's name.\n";
	}
	if((document.getElementById('applicationProjects').value == "Other") && (document.getElementById('otherApp').value == ""))
	{
		errmsg = errmsg + "Please enter the other application's name.\n";
	}
	if((document.getElementById('department').value != "Project") && (document.getElementById('requestPriority').value == ""))
	{
		errmsg = errmsg + "Please select a request priority.";
	}
	var errlen = errmsg.length;
	if(errmsg.length!=0)
	{
		alert(errmsg);
		return false;
	}
	else
	{
		return true;
	}
}

function validateTaskForm() { 
	var errmsg = "";
	if(document.getElementById('problem').value == "")
	{
		errmsg = errmsg + "Please Enter a problem.\n";
	}
	if(document.getElementById('applicationProjects').value == "")
	{
		errmsg = errmsg + "Please select an application.\n";
	}
	if((document.getElementById('applicationProjects').value == "Other") && (document.getElementById('otherApp').value == ""))
	{
		errmsg = errmsg + "Please enter the other application's name.\n";
	}
	if(document.getElementById('requestPriority').value == "")
	{
		errmsg = errmsg + "Please select a request priority.";
	}
	var errlen = errmsg.length;
	if(errmsg.length!=0)
	{
		alert(errmsg);
		return false;
	}
	else
	{
		return true;
	}
}

function validatePriorityForm() { 
	var errmsg = "";
	if(document.getElementById('requestPriority').value == "")
	{
		errmsg = errmsg + "Please select a request priority.\n";
	}
	var errlen = errmsg.length;
	if(errmsg.length!=0)
	{
		alert(errmsg);
		return false;
	}
	else
	{
		return true;
	}
}

function validateProblemForm() { 
	var errmsg = "";
	if(document.getElementById('problem').value == "")
	{
		errmsg = errmsg + "Please enter the problem title.\n";
	}
	var errlen = errmsg.length;
	if(errmsg.length!=0)
	{
		alert(errmsg);
		return false;
	}
	else
	{
		return true;
	}
}

function validateCommentForm() { 
	var errmsg = "";
	if(document.commentForm.commentDesc.value == "")
	{
		errmsg = errmsg + "Please enter a comment.\n";
	}
	var errlen = errmsg.length;
	if(errmsg.length!=0)
	{
		alert(errmsg);
		return false;
	}
	else
	{
		return true;
	}
}

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 listMoveSelectedItems(sourceName, targetName, valueFieldName, hiddenFieldName) {
	var usaCountryMetaValue = 259;
	var fromElem = MM_findObj(sourceName);
	var toElem = MM_findObj(targetName);
	var shouldAddItem = true;
	
	// add the selected to the target
	for(i=0; i<fromElem.length; i++) {	
		shouldAddItem = true;
		
		if(fromElem.options[i].selected) {
			//skip blank/helper elements
			if(fromElem.options[i].value != "") {
				
				// skip exising elements (they have the same values)
				for(j=0; j<toElem.length; j++) {
					if(fromElem.options[i].value == toElem.options[j].value) {
						shouldAddItem = false;
						break;
					}
				}
				if(shouldAddItem) {
					toElem.options[toElem.length] = new Option(fromElem.options[i].text, fromElem.options[i].value);
				}
				if(fromElem.options[i].value == usaCountryMetaValue && sourceName == 'country')
				{
					document.getElementById('regionDiv').style.display='block';
				}
				if(fromElem.options[i].value == usaCountryMetaValue && sourceName == 'selected_country_list')
				{
					document.getElementById('regionDiv').style.display='none';
				}
			}
		}
	}
	
	// delete the selected from the source
	for(i=fromElem.length-1; i>=0; i--) {
		if(fromElem.options[i].selected){
			//skip blank/helper elements
			if(fromElem.options[i].value != "") {
				fromElem.options[i] = null;
			}
		}
	}
	
	listUpdateHiddenField(valueFieldName, hiddenFieldName);
}

function listMoveAll(sourceName, targetName, valueFieldName, hiddenFieldName) {
	var fromElem = MM_findObj(sourceName);
	var toElem = MM_findObj(targetName);
	if(hiddenFieldName == 'countriesHiddenField')
	{
		document.getElementById('regionDiv').style.display='none';
	}
	
	// add them to the target
	for(i=0; i<fromElem.length; i++) {
		//skip blank/helper elements
		if(fromElem.options[i].value != "") {
			toElem.options[toElem.length] = new Option(fromElem.options[i].text, fromElem.options[i].value);
		}
	}
	
	// delete from the source
	for(i=fromElem.length-1; i>=0; i--) {
		//skip blank/helper elements
		if(fromElem.options[i].value != "") {
			fromElem.options[i] = null;
		}
	}
	
	listUpdateHiddenField(valueFieldName, hiddenFieldName);
	
	
}

function listClear(targetName, valueFieldName, hiddenFieldName) {
	var targetElem = MM_findObj(targetName);
	
	for(i=targetElem.length=1; i>=0; i--) {
		//skip blank/helper elements
		if(fromElem.options[i].value != "") {
			targetElem.options[i] = null;
		}
	}
	
	listUpdateHiddenField(valueFieldName, hiddenFieldName);
}

function listUpdateHiddenField(sourceName, hiddenFieldName) {
	var fromElem = MM_findObj(sourceName);
	var hiddenField = MM_findObj(hiddenFieldName);
	
	//reset it
	hiddenField.value = "";
	
	for(i=0; i<fromElem.length; i++) {
		if(i==0) {
			hiddenField.value = fromElem.options[i].value;
		}
		else {
			// In order to avoid adding any blank elements
			if(hiddenField.value == "") {
				hiddenField.value += fromElem.options[i].value;
			}
			else {
				hiddenField.value = hiddenField.value + "," + fromElem.options[i].value;
			}
		}
	}
}


function listAddElement(id, value, targetName, hiddenFieldName) {
	var toElem = window.opener.document.getElementById(targetName);
	
	toElem.options[toElem.length] = new Option(value, id);
	listUpdateHiddenField(targetName, hiddenFieldName);
}

function trim(str) 
{
	while(str.charAt(0) == (" ") ) 
	{
		str = str.substring(1); 
	} 
	while(str.charAt(str.length-1) == " " ) 
	{
	str = str.substring(0,str.length-1); 
	} 
	return str; 
} 

function convertHeadline()
{
	var uni = 0;
	var t=document.getElementById('title').value;

	var lf=false, cr=false, crlf='';
	if (t.indexOf(String.fromCharCode(10))!=-1) //line feed
	{
		lf=true;
		crlf+=String.fromCharCode(10);
	}
	if (t.indexOf(String.fromCharCode(13))!=-1) //carriage return
	{
		cr=true;
		crlf+=String.fromCharCode(13);
	}

	t=t.replace(/\xa0/g,' '); //nbsp
	t=t.replace(/\xb7/g,'&middot;'); //middot
	t=t.replace(/\xa9/g,'&copy;'); //copyright
	t=t.replace(/\xe9/g,'&eacute;'); //eacute
	
	var temp='';
	var cc;
	for(var c=0; c<t.length; c++)
	{
		cc=t.charCodeAt(c);
		if ((cc>160) && (cc<256)) temp+='&#'+cc+';';
		else if (cc<127) temp+=t.charAt(c);
		else if (uni) //unicode option
		{
			if (cc>255) temp+='&#'+cc+';';
		}
		else
		{
			switch (cc)
			{
				case 8220: temp+='&quot;'; break; //curly double quotes
				case 8221: temp+='&quot;'; break;
				case 132: temp+='&quot;'; break;
				case 147: temp+='&quot;'; break;
				case 148: temp+='&quot;'; break;
				case 8216: temp+="'"; break; //curly single quotes
				case 8217: temp+="'"; break;
				case 130: temp+="'"; break;
				case 145: temp+="'"; break;
				case 146: temp+="'"; break;
				case 131: temp+='f'; break; //function of, florin
				case 8230: temp+='...'; break; //ellipsis
				case 133: temp+='...'; break;
				case 8226: temp+='&middot;'; break; //middot
				case 149: temp+='&middot;'; break;
				case 8211: temp+='-'; break; //en/em dashes
				case 8212: temp+='-'; break;
				case 150: temp+='-'; break;
				case 151: temp+='-'; break;
				case 8482: temp+='<sup>TM</sup>'; break; //trademark
				case 153: temp+='<sup>TM</sup>'; break;
				case 8224: temp+='<sup>+</sup>'; break; //dagger
				case 134: temp+='<sup>+</sup>'; break;
				case 8225: temp+='<sup>++</sup>'; break; //double dagger
				case 135: temp+='<sup>++</sup>'; break;
				case 8249: temp+='&lt;'; break; //left single angle quote mark
				case 139: temp+='&lt;'; break;
				case 8250: temp+='&gt;'; break; //right single angle quote mark
				case 155: temp+='&gt;'; break;
				case 8240: temp+=' per 1000'; break; //per thousand
				case 137: temp+=' per 1000'; break;
				case 352: temp+='S'; break; //capital S caron
				case 138: temp+='S'; break;
				case 338: temp+='OE'; break; //capital OE ligature
				case 140: temp+='OE'; break;
				case 353: temp+='s'; break; //small s caron
				case 154: temp+='s'; break;
				case 339: temp+='oe'; break; //small oe ligature
				case 156: temp+='oe'; break;
				case 376: temp+='Y'; break; //capital Y dieresis
				case 159: temp+='Y'; break;
				case 152: temp+='~'; break; //small spacing tilde
			}
		}
	}
	t=temp;

	t=t.replace(/[\x7f-\x9f]/g,'_'); //strip other invalid chars
	
	// t=t.replace(/[\x00-\x1f]/g,' '); //control characters
	t=t.replace(/<\/p>/g,'</p>'+crlf); //nice formatting
	t=t.replace(/<br>/g,'<br>'+crlf);
	document.getElementById('convertedHeadline').value=t;
	
	
	var t=document.getElementById('originalTitle').value;
	lf=false; cr=false; crlf='';
	if (t.indexOf(String.fromCharCode(10))!=-1) //line feed
	{
		lf=true;
		crlf+=String.fromCharCode(10);
	}
	if (t.indexOf(String.fromCharCode(13))!=-1) //carriage return
	{
		cr=true;
		crlf+=String.fromCharCode(13);
	}

	t=t.replace(/\xa0/g,' '); //nbsp
	t=t.replace(/\xb7/g,'&middot;'); //middot
	t=t.replace(/\xa9/g,'&copy;'); //copyright
	t=t.replace(/\xe9/g,'&eacute;'); //eacute
	
	temp='';
	for(var c=0; c<t.length; c++)
	{
		cc=t.charCodeAt(c);
		if ((cc>160) && (cc<256)) temp+='&#'+cc+';';
		else if (cc<127) temp+=t.charAt(c);
		else if (uni) //unicode option
		{
			if (cc>255) temp+='&#'+cc+';';
		}
		else
		{
			switch (cc)
			{
				case 8220: temp+='&quot;'; break; //curly double quotes
				case 8221: temp+='&quot;'; break;
				case 132: temp+='&quot;'; break;
				case 147: temp+='&quot;'; break;
				case 148: temp+='&quot;'; break;
				case 8216: temp+="'"; break; //curly single quotes
				case 8217: temp+="'"; break;
				case 130: temp+="'"; break;
				case 145: temp+="'"; break;
				case 146: temp+="'"; break;
				case 131: temp+='f'; break; //function of, florin
				case 8230: temp+='...'; break; //ellipsis
				case 133: temp+='...'; break;
				case 8226: temp+='&middot;'; break; //middot
				case 149: temp+='&middot;'; break;
				case 8211: temp+='-'; break; //en/em dashes
				case 8212: temp+='-'; break;
				case 150: temp+='-'; break;
				case 151: temp+='-'; break;
				case 8482: temp+='<sup>TM</sup>'; break; //trademark
				case 153: temp+='<sup>TM</sup>'; break;
				case 8224: temp+='<sup>+</sup>'; break; //dagger
				case 134: temp+='<sup>+</sup>'; break;
				case 8225: temp+='<sup>++</sup>'; break; //double dagger
				case 135: temp+='<sup>++</sup>'; break;
				case 8249: temp+='&lt;'; break; //left single angle quote mark
				case 139: temp+='&lt;'; break;
				case 8250: temp+='&gt;'; break; //right single angle quote mark
				case 155: temp+='&gt;'; break;
				case 8240: temp+=' per 1000'; break; //per thousand
				case 137: temp+=' per 1000'; break;
				case 352: temp+='S'; break; //capital S caron
				case 138: temp+='S'; break;
				case 338: temp+='OE'; break; //capital OE ligature
				case 140: temp+='OE'; break;
				case 353: temp+='s'; break; //small s caron
				case 154: temp+='s'; break;
				case 339: temp+='oe'; break; //small oe ligature
				case 156: temp+='oe'; break;
				case 376: temp+='Y'; break; //capital Y dieresis
				case 159: temp+='Y'; break;
				case 152: temp+='~'; break; //small spacing tilde
			}
		}
	}
	t=temp;

	t=t.replace(/[\x7f-\x9f]/g,'_'); //strip other invalid chars
	
	// t=t.replace(/[\x00-\x1f]/g,' '); //control characters
	t=t.replace(/<\/p>/g,'</p>'+crlf); //nice formatting
	t=t.replace(/<br>/g,'<br>'+crlf);
	document.getElementById('convertedOriginalHeadline').value=t;
	
	return true;
}



function checkTitleLength()
{
	var title = document.getElementById('title').value;
	var originalTitle = document.getElementById('originalTitle').value;
	var msg = "";
	
	if(trim(title).length > 256)
	{
		msg = msg + "*Title should not be more than 256 characters.\n"
	}
	
	if(trim(originalTitle).length > 256)
	{
		msg = msg + "*Original Title should not be more than 256 characters.\n"
	}
	
	if(trim(msg) != "")
	{
		alert(msg);
		return false;
	}
	else
	{
		convertHeadline();
		return true;
	}
}


function displayWebsiteBoxOrPDFUpload(xLinkType)
{	
	if(xLinkType == "websiteURL")
	{
		document.getElementById('websiteDiv').style.display = '';
		document.getElementById('uploadPDFDiv').style.display = 'none';
	}
	else if(xLinkType == "uploadPDF")
	{
		document.getElementById('uploadPDFDiv').style.display = '';
		document.getElementById('websiteDiv').style.display = 'none';
	}
	else
	{
		document.getElementById('websiteDiv').style.display = 'none';
		document.getElementById('uploadPDFDiv').style.display = 'none';
	}
}

function checkCountry()
{	
	var selectedCountries;
	selectedCountries = document.getElementById('countriesHiddenField').value;
	if(trim(selectedCountries) == '')
	{
		alert('*Please select Country');
		return false;
	}
	else
		return true;
}

function checkIfBetweenSelected() 
{			
	var dateCompareOperatorValue = document.getElementById('dateCompareOperator').options[document.getElementById('dateCompareOperator').selectedIndex].value;
	
	if(dateCompareOperatorValue!='Between')	
	{	
		document.getElementById('searchToDateDiv').style.display = 'none';
	}
	else
	{	
		document.getElementById('searchToDateDiv').style.display = 'block';
	}
} 

/* END: iiSupport and Project management Javascript */