/**
 * Add State Module JS Files
 * @author Pathfinder Solutions India
 * @link http://www.pathfindersolutions.biz
 * @version 1.0
 * @package justmeans
 * @subpackage state
 */

/*
*	FOR AJAX CALLS OBJECTS
*/
var ajaxObjects = new Array();

function fnShowInfoLoadingImage(strContainerHtml)
{
	document.getElementById(strContainerHtml).innerHTML = "<div style='text-align:center; padding: 40px;'><span style='font-size: 20px; font-weight: bold; color: #AAAAAA;'>Loading...</span><br /><br /><img src='images/big_slide_loader.gif' align='absmiddle'></div>";
}

function fnHideShowTab(intTabId, intTabCount)
{
	var enmIssetForm;
	var strSetFormName;
	var intValidTabNo;
	
	enmIssetForm = parseInt(document.getElementById("id_enmIssetForm").value);
	strSetFormName = document.getElementById("id_strSetFormName").value;
	
	intValidTabNo = document.getElementById("id_intValideTabId").value;
	
	if (enmIssetForm == 1)
	{
		if (confirm("Do you want to save the changes you made?"))
		{
			if (strSetFormName == "frmContactPerson")
			{
				fnValidateContactPersonData();
				return false;
			}
			else if (strSetFormName == "frmUploadJobOpening")
			{
				errordiv = "id_ErrorMessage_JobOpening";
				if (fnValidateForm(window.frames[0].document.getElementById("frmUploadJobOpening")))
				{
					window.frames[0].document.frmUploadJobOpening.submit();
					return false;
				}
				else
				{
					fnScrollWindow("id_TabHead_2");
					fnResetFrameHeight(0);
					return false;
				}
			}
			else if (strSetFormName == "preview")
			{
				if (intTabId == 4)
					fnShowPaymentTab();
			}
			else if (strSetFormName == "frmPayContactInfo")
			{
				fnValidatePayContactInfo();
				return false;
			}
			//intTabId = (document.getElementById("id_intValideTabId").value + 1);
			//fnShowPaymentTab
		}
		else if (intTabId > intValidTabNo)
		{
			ajax_showTooltip("id_TabHeadError",document.getElementById("id_TabIcon_"+intTabId),300,55);
			return false;
		}
	}
	else
	{
		if (intTabId > intValidTabNo)
		{
			ajax_showTooltip("id_TabHeadError",document.getElementById("id_TabIcon_"+intTabId),300,55);
			return false;
		}
	}
	
	for (intCount = 1; intCount <= intTabCount; intCount++)
	{
		if (intCount == intTabId)
		{
			// CHANGE TAB AND TAB ICON DISPLAY
			document.getElementById("id_Tab_"+intCount).style.display = "";
			document.getElementById("id_TabIcon_"+intCount).src = "images/ico_exp_old.gif";
			
			// FUNCTION TO SCROLL THE WIDOW AND POIN TAB HEADER
			fnScrollWindow("id_TabHead_"+intCount);
		}
		else
		{
			document.getElementById("id_Tab_"+intCount).style.display = "none";
			document.getElementById("id_TabIcon_"+intCount).src = "images/ico_coll_old.gif";
		}
	}
	
	if (intTabId == 2)
		fnResetFrameHeight(0);
	
}

function fnCancelAction(strFormName)
{
	if (strFormName != "")
		document.getElementById(strFormName).reset();
}

function fnValidateContactPersonData()
{
	errordiv = "id_ErrorMessage";

	if (fnValidateForm(document.getElementById("frmContactPerson")))
	{
		document.getElementById("id_Tab_1").style.display = "none";
		document.getElementById("id_Tab_2").style.display = "";
		fnShowInfoLoadingImage("id_Tab_2");
		
		strContactFname = document.getElementById("id_txtFirstName").value;
		strContactLname = document.getElementById("id_txtLastName").value;
		strContactPhone = document.getElementById("id_txtPhoneNo").value;
		strContactEmail = document.getElementById("id_txtEmail").value;
		
		strEncContactFname = Base64.encode(strContactFname);
		strEncContactLname = Base64.encode(strContactLname);
		strEncContactPhone = Base64.encode(strContactPhone);
		strEncContactEmail = Base64.encode(strContactEmail);
		
		if(parseInt(document.getElementById("id_hdnJobId").value) > 0)
		{
			var jobId = document.getElementById("id_hdnJobId").value;
			
			ajaxObjects[0] = new sack();
			ajaxObjects[0].requestFile = sitename+"/index.php?action=post-job-request&mode=ajax&ajax_action=savecontactinfo&fname="+strEncContactFname+"&lname="+strEncContactLname+"&phone="+strEncContactPhone+"&email="+strEncContactEmail+"&job="+jobId;
			ajaxObjects[0].onCompletion = function(){ 
															if (ajaxObjects[0].response == "INVALID OPERATION")
															{
																alert("INVALID OPERATION");
																return false;
															}
															else
															{
																document.getElementById("id_enmIssetForm").value = 0;
																document.getElementById("id_strSetFormName").value = "";
																
																document.getElementById("id_Tab_2").innerHTML = ajaxObjects[0].response;
																document.getElementById("id_intValideTabId").value = 2;
																
																fnHideShowTab(2, 4);
															}
													};
			ajaxObjects[0].runAJAX();
		}else{		
			ajaxObjects[0] = new sack();
			ajaxObjects[0].requestFile = sitename+"/index.php?action=post-job-request&mode=ajax&ajax_action=savecontactinfo&fname="+strEncContactFname+"&lname="+strEncContactLname+"&phone="+strEncContactPhone+"&email="+strEncContactEmail;
			ajaxObjects[0].onCompletion = function(){ 
															if (ajaxObjects[0].response == "INVALID OPERATION")
															{
																alert("INVALID OPERATION");
																return false;
															}
															else
															{
																document.getElementById("id_enmIssetForm").value = 0;
																document.getElementById("id_strSetFormName").value = "";
																
																document.getElementById("id_Tab_2").innerHTML = ajaxObjects[0].response;
																document.getElementById("id_intValideTabId").value = 2;
																
																fnHideShowTab(2, 4);
															}
													};
			ajaxObjects[0].runAJAX();
		}
	}
	return false;
}

function fnValidatePayContactInfo()
{  
	errordiv = "id_PaymentErrorMessage";
	
	if (fnValidateForm(document.getElementById("frmPayContactInfo")))
	{ 
		strPaymentMethod = "";
		if (document.getElementById("id_ChkInvoice").checked == true && document.getElementById("id_ChkCreditCard").checked == false)
			strPaymentMethod = Base64.encode("invoice");
		else if (document.getElementById("id_ChkInvoice").checked == false && document.getElementById("id_ChkCreditCard").checked == true)
			strPaymentMethod = Base64.encode("creditcard");
			
		intJobId 			= document.getElementById("id_hdnJobId").value;
		strContactFname 	= Base64.encode(document.getElementById("id_PayContactFname").value);
		strContactLname 	= Base64.encode(document.getElementById("id_PayContactLname").value);
		intOrganizationId 	= Base64.encode(document.getElementById("txtOrganizationId").value);
		strOrganizationName = Base64.encode(document.getElementById("txtOrganizationName").value);
		strContactEmail		= Base64.encode(document.getElementById("id_PayContactEmail").value);
		strContactPhone		= Base64.encode(document.getElementById("id_PayContactPhone").value);
		strContactAltPhone	= Base64.encode(document.getElementById("id_PayContactAltphone").value);
		
		strUrl = "fname="+strContactFname+"&lname="+strContactLname+"&orgid="+intOrganizationId+"&orgname="+strOrganizationName+"&email="+strContactEmail;
		strUrl = strUrl+"&phone="+strContactPhone+"&altphone="+strContactAltPhone+"&paymentmethod="+strPaymentMethod;
		
		ajaxObjects[1] = new sack();
		ajaxObjects[1].requestFile = sitename+"/index.php?action=post-job-request&mode=ajax&ajax_action=savepaymentinfo&job="+intJobId+"&"+strUrl;
		//alert(ajaxObjects[1].requestFile); return false;
		ajaxObjects[1].onCompletion = function(){ 	
		
												if (parseInt(ajaxObjects[1].response) == 1)
													{
														document.getElementById("id_Tab_4").innerHTML = document.getElementById("id_ConfirmationText").innerHTML;
													}
												
													document.getElementById("id_enmIssetForm").value = 0;
													document.getElementById("id_strSetFormName").value = "";
													
													fnResetPate();
												};
		ajaxObjects[1].runAJAX();
	}
	fnScrollWindow("id_TabHead_4");
}

function fnReloadOldPost()
{
	intOldPostId = fnGetCheckedValue(document.forms['frmUploadLiabrary'].elements['optPastJob']);
	
	target = document.getElementById("id_frmUploadJob");
	target.src = sitename+"/index.php?action=uploadjobhtml&jobid="+intOldPostId;
}

function fnGetCheckedValue(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 fnShowPaymentTab()
{
	document.getElementById("id_Tab_3").style.display = "none";
	document.getElementById("id_Tab_4").style.display = "";
	fnShowInfoLoadingImage("id_Tab_4");
	
	intJobId = document.getElementById("id_hdnJobId").value;
	
	ajaxObjects[1] = new sack();
	ajaxObjects[1].requestFile = sitename+"/index.php?action=post-job-request&mode=ajax&ajax_action=showpaymenttab&job="+intJobId;
	ajaxObjects[1].onCompletion = function(){ 
												document.getElementById("id_Tab_4").innerHTML = ajaxObjects[1].response;
												try{
													eval('txtOrganizationName = "txtOrganizationId";');
													autoCom.add("txtOrganizationName", sitename+"/modules/company/inc/ajax_companies.php");
												}
												catch(e)
												{}
												document.getElementById("id_intValideTabId").value = 4;
												
												document.getElementById("id_enmIssetForm").value = 4;
												document.getElementById("id_strSetFormName").value = "frmPayContactInfo";
												
												fnAddChangeFunction(document.getElementById('frmPayContactInfo'));
												
												fnHideShowTab(4, 4);
											};
	ajaxObjects[1].runAJAX();
}

function fnResetPate()
{
	document.getElementById("id_intValideTabId").value = 1;
	document.getElementById("id_hdnJobId").value = 0;

	document.getElementById("id_Tab_2").innerHTML = "";
	document.getElementById("id_Tab_3").innerHTML = "";

	ajaxObjects[2] = new sack();
	ajaxObjects[2].requestFile = sitename+"/index.php?action=post-job-request&mode=ajax&ajax_action=showcontactperson";
	ajaxObjects[2].onCompletion = function(){ 
												document.getElementById("id_Tab_1").innerHTML = ajaxObjects[2].response;
											};
	ajaxObjects[2].runAJAX();
}

function fnAddChangeFunction(obj)
{
	var frm = obj;
	var len = frm.elements.length;
	
	for(var i=0 ; i<len ; i++) 
	{		
		var eletype = frm.elements[i].type;
		var ObjElement = frm.elements[i];

		if( (eletype.toLowerCase() != "submit") && (eletype.toLowerCase() != "hidden"))
		{
			ObjElement.setAttribute("onchange","fnActivateChangeFlag('"+obj.name+"')");
		}
	}
}

function fnActivateChangeFlag(strFormName)
{
	document.getElementById("id_enmIssetForm").value = 1;
	document.getElementById("id_strSetFormName").value = strFormName;
}

function fnResetFrameHeight(intExtrasize)
{
	intHeight = parent.document.getElementById("id_frmUploadJob").contentWindow.document.body.scrollHeight;
	intHeight = intHeight + intExtrasize;
	parent.document.getElementById("id_frmUploadJob").style.height = intHeight + "px";
}

function fnShowEditJobInfo()
{
	var enmOldIssetForm = parseInt(document.getElementById("id_enmIssetForm").value);
	var strOldSetFormName = document.getElementById("id_strSetFormName").value;
	
	document.getElementById("id_enmIssetForm").value = 0;
	document.getElementById("id_strSetFormName").value = "";
	
	fnHideShowTab(2, 4);
	
	fnResetFrameHeight(0);
	
	document.getElementById("id_enmIssetForm").value = enmOldIssetForm;
	document.getElementById("id_strSetFormName").value = strOldSetFormName;
}