var jsf=true;
var jsp=false;
var rsf=true;
var rsp=false;
var cnt=0;
var temp=0;
var rstableId=0;
var jptableId=0;
var chkArr;
var http = getHTTPObject();
var http2= getHTTPObject();
var result1;
var available;
var notAvailable;

function assign(errorDiv, helperMsg){
		document.getElementById(errorDiv).innerHTML='<font color=red>'+helperMsg+'</font>';
		return false;
}	

function disableCreditPeriod()
{
	document.getElementById('creditPeriod').value=-1;
	document.getElementById('creditPeriod').style.visibility="hidden";
	document.getElementById('creditErrorMsg').innerHTML="";
	document.getElementById('creditlabel').style.visibility="hidden";
}
function enableCreditPeriod()
{
	document.getElementById('creditPeriod').value=-1;
	document.getElementById('creditPeriod').style.visibility="visible";
	document.getElementById('creditlabel').style.visibility="visible";
}
function isFreeService(obj)
{
	
	if(obj.value==2)
	{
		
		document.getElementById('costPerUnit').value=0;
		document.getElementById('currencyCode').style.visibility="hidden";
		document.getElementById('costPerUnit').style.visibility="hidden";
		document.getElementById('costlabel').style.visibility="hidden";
		document.getElementById('costErrorMsg').innerHTML="";
	}
	else
	{
		document.getElementById('costPerUnit').value="";
		document.getElementById('currencyCode').style.visibility="visible";
		document.getElementById('costPerUnit').style.visibility="visible";
		document.getElementById('costlabel').style.visibility="visible";
	}
	
	if(obj.value==0)
	{
		enableCreditPeriod();
	}
	else
	{
		disableCreditPeriod();
	}
	

}
function package_popup(path)
{

  	mywindow = window.open (path,"mywindow","location=0,status=0,scrollbars=0,width=200,height=150,directories=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=0");
 	mywindow.moveTo(200,200);
}
function popup_VAL()
{

  	mywindow = window.open ("./jsp/validity_desc.html","mywindow","location=0,status=0,scrollbars=0,width=150,height=40,directories=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=0");
 	mywindow.moveTo(200,200);
}
function writeDescription(obj,id)
{
  document.getElementById('popUpDescription').value=id;
  mywindow = window.open ("./jsp/package_desc_more.jsp",
  "mywindow","location=0,status=0,scrollbars=0,width=400,height=300,directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=0");
  mywindow.moveTo(200,200);
}

function clearErrorMsg(alert)
{
	document.getElementById(alert).innerHTML="";
}

function RemoveErrorMsg()
{
	document.getElementById('serviceErrorMsg').innerHTML="";
	document.getElementById('categoryErrorMsg').innerHTML="";
	document.getElementById('userTypeErrorMsg').innerHTML="";
	document.getElementById('offerAsErrorMsg').innerHTML="";
	document.getElementById('creditErrorMsg').innerHTML="";
	document.getElementById('activDateErrorMsg').innerHTML="";
	document.getElementById('validityPeriodErrorMsg').innerHTML="";
	//document.getElementById('currencyErrorMsg').innerHTML="";
	document.getElementById('costErrorMsg').innerHTML="";
	document.getElementById('offerErrorMsg').innerHTML="";
	if(document.getElementById('jobPost').value=='YES')
	{
		document.getElementById('errMsg1').innerHTML="";
		document.getElementById('errMsg2').innerHTML="";
		document.getElementById('errMsg3').innerHTML="";
		document.getElementById('errMsg4').innerHTML="";
	}
	if(document.getElementById('resumeSearch').value=='YES')	
		document.getElementById('errMsg5').innerHTML="";
	
}



function isString(elem,errorDiv, helperMsg){
	var numericExpression = /^[,\na-z A-Z]+$/;
	if(trim(document.getElementById(elem).value).match(numericExpression)){
		return "";
	}else{
		document.getElementById(errorDiv).innerHTML='<font color=red>'+helperMsg+'</font>';
		return false;
	}
}
function isDate(elem)
{
	var numericExpression = /^[-0-9]+$/;
	if(trim(elem).match(numericExpression)){
		return true;
	}else{
		return false;
	}
}
function isNumber(elem,errorDiv, helperMsg){
	var numericExpression = /^[.0-9]+$/;
	if(trim(document.getElementById(elem).value).match(numericExpression)){
		return "";
	}else{
		document.getElementById(errorDiv).innerHTML='<font color=red>'+helperMsg+'</font>';
		return false;
	}
}
function checkNumber(value){
	var numericExpression = /^[.0-9]+$/;
	if(trim(value).match(numericExpression)){
		return true;
	}else{
		return false;
	}
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}


function dynamicCreation(param,param1)
{

	
	var JPS=document.getElementById('jobPostingSize').value;
	var RSS=document.getElementById('resumeSearchSize').value;
	chkArr=document.getElementsByName(param);
	var mainTable=document.getElementById('InsertRow');
	var tLen=mainTable.rows.length;
	
	var serdesc=document.getElementsByName('serviceDescription');
	var pacdesc=document.getElementById('description');
	var selectedServiceId="";
	var idLength=0;
	
	var tempDesc="";
	for(i=0;i<serdesc.length;i++)
	{
		if(chkArr[i].checked==true)
		{
			tempDesc+=serdesc[i].value+",";
			selectedServiceId+=chkArr[i].value+",";
			idLength++;
		}
	}
	if(param1=='second')
	{
		pacdesc.value="";
		pacdesc.value=tempDesc.substring(0,tempDesc.length-1);
	}	
	
document.getElementById('selectedServiceId').value=selectedServiceId;
document.getElementById('idLength').value=idLength;	
if(JPS!=null && JPS>0)
{	
	for(i=0;i<JPS;i++)
	{
			
		if(chkArr[i].checked==true)
		{
			cnt=0;
			break;
		}
		if(chkArr[i].checked==false )
		{
			cnt=1;
		}
	}
	if(jsf==true && jsp==false)
	{
		var ids=document.getElementById('InsertRow');
		var r=ids.insertRow(ids.rows.length);
		jptableId=ids.rows.length;
		var c1=r.insertCell(0);
		var c2=r.insertCell(1);
		var c3=r.insertCell(2);
		var c4=r.insertCell(3);
		c1.height=35;
		c1.align="right";
		c1.innerHTML="<div><br></div><div><br></div><font color='red'>*</font> Job Posting Limits";
		c2.innerHTML="";
		c3.innerHTML="<div><br></div><div><b>Job Posting</b><br></div><div class='textMain' id='errMsg1'><br></div><input type='text' class='textfield' name='jobPostingLimits' id='jobPostingLimits' value='0'>";
		c4.innerHTML="";
		
		var ids=document.getElementById('InsertRow')
		var r=ids.insertRow(ids.rows.length);
		var c1=r.insertCell(0);
		var c2=r.insertCell(1);
		var c3=r.insertCell(2);
		var c4=r.insertCell(3);
		c1.height=35;
		c1.align="right";
		c2.innerHTML="";
		c1.innerHTML="<font color='red'>*</font> Visibility Period";
		c3.innerHTML="<div id='errMsg2'><br></div> <input type='text' name='visibilityPeriod' id='visibilityPeriod' class='textfield' value='0'>&nbsp;<a href='#' onclick=package_popup('./jsp/jobposting_visibility_desc.html')><font color='red'>?</font></a>";
		c4.innerHTML="";

		var ids=document.getElementById('InsertRow')
		var r=ids.insertRow(ids.rows.length);
		var c1=r.insertCell(0);
		var c2=r.insertCell(1);
		var c3=r.insertCell(2);
		var c4=r.insertCell(3);
		c1.height=35;
		c1.align="right";
		c2.innerHTML="";
		c1.innerHTML="<font color='red'>*</font> Description Limits";
		c3.innerHTML="<div id='errMsg3'><br></div><input type='text' id='descriptionLimits' name='descriptionLimits' class='textfield' value='0'>&nbsp;<a href='#' onclick=package_popup('./jsp/character_limit_desc.html')><font color='red'>?</font></a>";
		c4.innerHTML="";
		
		var ids=document.getElementById('InsertRow')
		var r=ids.insertRow(ids.rows.length);
		var c1=r.insertCell(0);
		var c2=r.insertCell(1);
		var c3=r.insertCell(2);
		var c4=r.insertCell(3);
		c1.height=35;
		c1.align="right";
		c2.innerHTML="";
		c1.innerHTML="<font color='red'>*</font> Email Limits";
		c3.innerHTML="<div id='errMsg4'><br></div><input type='text' id='emailLimits' name='emailLimits' class='textfield' value='0'>&nbsp;<a href='#' onclick=package_popup('./jsp/email_limit_desc.html')><font color='red'>?</font></a>";
		c4.innerHTML="";
		jsf=false;
		jsp=true;
		document.getElementById('jobPost').value="YES";
	}
	if(jsp==true && jsf==false && cnt==1)
	{
		var ids=document.getElementById('InsertRow')
		var r=ids.deleteRow(jptableId-1);
		var r=ids.deleteRow(jptableId-1);
		var r=ids.deleteRow(jptableId-1);
		var r=ids.deleteRow(jptableId-1);
		jsf=true;
		jsp=false;
		cnt=0;
		jptableId=0;
		document.getElementById('jobPost').value="NO";
	}
	
}
if(RSS!=null && RSS>0)
{
	for(i=JPS;i<chkArr.length;i++)
	{
		if(chkArr[i].checked==true)
		{
			temp=0;
			break;
		}
		if(chkArr[i].checked==false )
		{
			temp=1;
	
		}
	}
	if(rsf==true && rsp==false)
	{
		var ids=document.getElementById('InsertRow')
		var r=ids.insertRow(ids.rows.length);
		rstableId=ids.rows.length;
		var c1=r.insertCell(0);
		var c2=r.insertCell(1);
		var c3=r.insertCell(2);
		var c4=r.insertCell(3);
		c1.height=35;
		c1.align="right";
		c2.innerHTML="";
		c1.innerHTML="<div><br></div><div><br></div><font color='red'>*</font> Resume Download Limit";
		c3.innerHTML="<div><br></div><div><b>Resume Search</b><br></div><div class='textMain' id='errMsg5'><br></div><input type='text' class='textfield' id='resumeDownloadLimits' name='resumeDownloadLimits' value='0'>&nbsp;<a href='#' onclick=package_popup('./jsp/resume_download_limit_desc.html')><font color='red'>?</font></a>";
		c4.innerHTML="";
		rsf=false;
		rsp=true;
		document.getElementById('resumeSearch').value="YES";
	}
	if(rsp==true && rsf==false && temp==1)
	{
		ids=document.getElementById('InsertRow');
		var r=ids.deleteRow(rstableId-1);
		rsf=true;
		rsp=false;
		temp=0;
		rstableId=0;
		document.getElementById('resumeSearch').value="NO";
	}
}

}

function deSelect(id)
{
	var subUserLicence = document.getElementById(id); 
	if(subUserLicence != null)
	{
		subUserLicence.checked=false;
	}
}
function checkDuplicatePackageAndServiceName(value1,value2) 
{
	
	available=value1;
	notAvailable=value2;
	var name = document.getElementById("name").value;
	if(trim(name).length>0)
	{
		http = getHTTPObject(); // We create the XMLHTTPRequest Object
		var url = "PopulateDynamics?name="; // The server-side script
		http.open("GET", url + escape(name), true);
		http.onreadystatechange = handleHttpResponseForOfferName;
		http.send(null);
	}	
	
}

function handleHttpResponseForOfferName() {
	if (http.readyState == 4) {
		if (http.status == 200) {
			var name = document.getElementById("name");
            var message = http.responseXML.getElementsByTagName("offer")[0];
            var result = message.childNodes[0].nodeValue;
            if(result == "AVAILABLE"){               	
              	document.getElementById("nameErrorMsg").innerHTML="<font color='green'>"+available+"</font>";
              	document.getElementById("nameDuplicate").value="AVAILABLE";
              	return true;
            }
            else{                    
               	document.getElementById("nameErrorMsg").innerHTML="<font color='red'>"+notAvailable+"</font>";
               	document.getElementById("nameDuplicate").value="NOTAVAILABLE";
               	return false;
            }
       }
       else  if ( http.status != 0 ) {
           	alert("There was an error while checking Service / Package Name availability.\nPlease try again.");
      }
   }	

}
function defaultLoading()
{
		document.getElementById('costPerUnit').value="";
		document.getElementById('currencyCode').style.visibility="visible";
		document.getElementById('costPerUnit').style.visibility="visible";
		document.getElementById('costlabel').style.visibility="visible";
		disableCreditPeriod();
}
function defaultLoadingEdit()
{

	var obj=document.getElementById('offeredAs');
	if(obj.value==2)
	{
		document.getElementById('costPerUnit').value=0;
		document.getElementById('currencyCode').style.visibility="hidden";
		document.getElementById('costPerUnit').style.visibility="hidden";
		document.getElementById('costlabel').style.visibility="hidden";
		
	}
	if(obj.value==0)
	{
		enableCreditPeriod();
	}
	else
	{
		disableCreditPeriod();
	}
}


function getHTTPObject() {
	//alert("In getHttpObject");
	var xmlhttp;
	if (window.XMLHttpRequest) {
		////alert("In first if condition");
		xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		//alert("In second if condition");
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} return xmlhttp;
}

/*The below functions are for Invoce related*/
function generatedFiledValue()
{
	var descriptionArr=document.getElementsByName("description");
	var quantityArr=document.getElementsByName("quantity");
	var unitCostArr=document.getElementsByName("unitCost");
	var subItemArr=document.getElementsByName("invoiceSubItemId");
	var tempdesc="";
	var tempqty="";
	var tempcost="";
	var tempSubId="";
	var countLen=0;
	var f1=false;
	var f2=false;
	var f3=false;
	var status;
	for(k=0;k<descriptionArr.length;k++)
	{
		
		if(trim(descriptionArr[k].value)!="" && trim(quantityArr[k].value)!="" && trim(unitCostArr[k].value)!="")
		{
			tempdesc+=descriptionArr[k].value+"^";
			if(!checkNumber(quantityArr[k].value))
			{
				status ="S&"+countLen+"-2";
				break;
			}
			if(!checkNumber(unitCostArr[k].value))
			{
				status ="S&"+countLen+"-3";
				break;
			}
			tempqty+=quantityArr[k].value+"^";
			tempcost+=unitCostArr[k].value+"^";
			tempSubId+=subItemArr[k].value+"^";
			countLen=countLen+1;
			status="Y&"+countLen;
		}
		else if(trim(descriptionArr[k].value)=="" && trim(quantityArr[k].value)=="" && trim(unitCostArr[k].value)=="")
		{	
			status="Y&"+countLen;
			break;
		}
		else
		{
			if(trim(descriptionArr[k].value)!="")
			{
				f1=true;
			}
			if(trim(quantityArr[k].value)!="")
			{
				f2=true;
			}
			if(trim(unitCostArr[k].value)!="")
			{
				f3=true;
			}
			if((f1)&&(f2)&&(f3))
			{
		
				status="Y&"+countLen;
				break;
			}
			else
			{
				status="P&"+countLen;
				break;
			}
		}
	}
	
	document.getElementById("descValue").value=tempdesc;
	document.getElementById("qtyValue").value=tempqty;
	document.getElementById("costValue").value=tempcost;
	document.getElementById("subItemId").value=tempSubId;
	document.getElementById("totalrow").value=countLen;
	
	return status;
}

function createTableRow()
{
			var ids=document.getElementById('InsertRow');
			var r=ids.insertRow(ids.rows.length);
			var c1=r.insertCell(0);
			var c2=r.insertCell(1);
			var c3=r.insertCell(2);
			c1.height=65;
			c1.align="center";
			c2.align="center";
			c3.align="center";
			c1.innerHTML="<textarea name='description' cols='30' rows='3' class='textfield' id='description'></textarea>"
						  +"<input type='hidden' name='invoiceSubItemId' id='invoiceSubItemId'>";
			c2.innerHTML="<input type='text' name='quantity' id='quantity' class='textfield' />";
			c3.innerHTML="<input type='text' name='unitCost' id='unitCost' class='textfield'/>";
}
function deleteTableRow()
{
	var ids=document.getElementById('InsertRow');
	if(ids.rows.length>7)
	{
		var r=ids.deleteRow(ids.rows.length-1);
	}

}

function setMethod(methodName)
{
	if(methodName=="create")
	{
		document.getElementById("method").value="createSubUserLicense";
	}
	else if(methodName=="save")
	{
		document.getElementById("method").value="saveSubUserLicense";
	}
	else if(methodName=="delete")
 	{
 		document.getElementById("method").value="deleteSubUserLicense";
	}
	
	//alert(methodName+" "+document.getElementById("method").value);
}

















var selectedList;
var availableList;
function createListObjects(availableOptions,selectedOptions)
{
    availableList = document.getElementById(availableOptions);
    selectedList = document.getElementById(selectedOptions);
}
function delAttribute(){
   var selIndex = selectedList.selectedIndex;
   if(selIndex < 0)
      return;
   availableList.appendChild(
      selectedList.options.item(selIndex))
   selectNone(selectedList,availableList);
}
function addAttribute(){
   var addIndex = availableList.selectedIndex;
   if(addIndex < 0)
      return;
   selectedList.appendChild( 
      availableList.options.item(addIndex));
   selectNone(selectedList,availableList);

}

function setSize(list1,list2){
    list1.size = getSize(list1);
    list2.size = getSize(list2);
}

function selectNone(list1,list2){
    list1.selectedIndex = -1;
    list2.selectedIndex = -1;
    addIndex = -1;
    selIndex = -1;
}
function delAll(){
    var len = selectedList.length -1;
    for(i=len; i>=0; i--){
        availableList.appendChild(selectedList.item(i));
    }
    selectNone(selectedList,availableList);
}

function addAll(){
    var len = availableList.length -1;
    for(i=len; i>=0; i--){
        selectedList.appendChild(availableList.item(i));
    }
    selectNone(selectedList,availableList);
   }
function getSize(list){
    /* Mozilla ignores whitespace,IE doesn't - count the elements in the list */
    var len = list.childNodes.length;
    var nsLen = 0;
   
    for(i=0; i<len; i++){
        if(list.childNodes.item(i).nodeType==1)
            nsLen++;
    }
    if(nsLen<2)
        return 2;
    else
        return nsLen;
}
