var timer=null;
var interval=30000; //30 secs
function reqTime(){
  timer=setInterval("work();",interval);
}
function work(){
	var dateObj = new Date(document.frmManage.hdnCurrent.value);
	document.frmManage.hdnCurrent.value=new Date(dateObj.getYear(), dateObj.getMonth(), dateObj.getDate(), dateObj.getHours(), dateObj.getMinutes(), dateObj.getSeconds()+ interval/1000);
}

function chgLookup(cbo){
	var HREF=document.frmManage.SvRtnAction.value;
	
	i=HREF.indexOf('&goto=') + 6;
	if (cbo[cbo.selectedIndex].className!='') 
	{
		HREFSearch.href = HREF.substring(0,i) +  cbo[cbo.selectedIndex].className + HREF.substr(i);
		if (cbo[cbo.selectedIndex].className==4000)
		{
		MM_showHideLayers('OpportunityLink','','show');
		}
		else{MM_showHideLayers('OpportunityLink','','hide')}
			
	}
	else HREFSearch.href= 'javascript:void(0)';
}


var log='';
function RequestTypeChg(obj,obj1){
	var scriptHTML="";
	if (typeof(obj.length)!='undefined'){
		for(var i=0;i<obj.length;i++){
			if(typeof(obj1)!='undefined' && obj[i].value==obj1.value) obj[i].checked=true;
			if (obj[i].checked)	scriptHTML= obj[i].className;
		}
	}
	else scriptHTML=obj.className; //onclick event for each RADIO item 
	log=GetValueByKey(scriptHTML,'login=');
	document.getElementById('lblRQ').innerHTML = (log)?'Requestor:':'';
	document.getElementById('txtRQ').innerHTML = (log)?'<input name=txtCustomer size=70/>':'';

	scriptHTML=GetValueByKey(scriptHTML,'script=',true)
	//if(scriptHTML.length>550) document.all.item('divScript').style.cssText="overflow: auto; width:720px; height:100px;";
	//else document.all.item('divScript').style.cssText="overflow: auto; width:720px; height:40px;";
	document.all.item('divScript').innerHTML= scriptHTML;
		
	//}
	//alert(document.frmManage.Add.href);
}
//!!!TODO: Client Time difference!!! ??Display Seconds?
function enterTime(obj){
	var d=new Date(document.frmManage.hdnCurrent.value);
	var iMinutes = d.getMinutes();
	obj.value=d.getHours() + ':' + (iMinutes>9?'':'0') + iMinutes; //+ ':' + d.getSeconds();
}			

function GetValueByKey(sInf,sKey,end){
	var iPos=0;
	var iPosStop=0;
		var sVal=""; //Return value
	iPos=sInf.indexOf(sKey);
	if (iPos==-1){
		sVal="";
	}
	else{
		sVal=sInf.substr(iPos+sKey.length,sInf.length);
		iPosStop= end?-1:sVal.indexOf(';');
			if (iPosStop==-1){
			//End of Info string
				sVal=sVal;
			}
			else{
				//Retrieve information
				sVal=sVal.substr(0,iPosStop);
			}
		}
   return sVal;
}

function FRMValidate(){
	var sStop="";
	if (typeof(document.frmManage.RequestTypeID)!='undefined'){
		var obj=document.frmManage.RequestTypeID;
		if (obj.length>0){
			sStop="stop";	
			for(var i=0;i<obj.length;i++){
				if (obj[i].checked){ sStop=""; break; }
			}
		}
		else if(!obj.checked) sStop="stop";
		if (sStop!=''){ alert('Please, select a Request Type'); obj[0].focus();}
	}
	if(typeof(document.frmManage.ApprovedByID)!='undefined' && !document.frmManage.ApprovedByID.checked){
		sStop='stop'; alert('Please, authorize the Work Order created')
	}
	return sStop;
}

function QuoteValidation(rid)
{
	var sUrl;
	var aInputs = document.all.item("chkItemStatus");

	var aCbos = new Array();
	if (document.all.item("chkItemStatus")) aCbos=document.all.item("chkItemStatus");
	
	for(var i = 0; i<aCbos.length; i++){		
	if (typeof(aCbos[i].name) !="undefined")	// if there is array from some comboboxes
		{
 		if (aInputs[i].value==1)	
	 		{
	 		alert('Item Status for all Items must be set to either Priced or Not Available before a quote can be generated'); 
			return;	
	 		}
	 	}
	else
		{
		 if (aCbos[i].selected && aCbos[i].value==1) 
		 {	 	
		alert('Item Status for all Items must be set to either Priced or Not Available before a quote can be generated'); 
		return;
		 }
		
		}	
	}
	
	_onSave(document.frmManage,'2334&action=4&id=' + rid + '&qid=-1');		
}

//////// Request Submit button - use on Public Page and Admin Entry 
function ReqSubmit(id, bAddDoc, bSts, curUrl)
{	

if (valRequired()==''){	
	
	document.frmManage.hdnActive.value=1;
	var urlcll="";
	var urlSave="";
	var urlRtn="";
	var goto='&state=goto&goto=';
	var DocPage='1082|action=16|id=' + id;
	
	if (curUrl==2334){
		urlSave='admin.asp?uri='+ curUrl + '&action=4&id=' +id;
		urlcll='|cll=2334|action=1|id=' + id + '|st=10';
		urlRtn='2334|action=1|id=' + id + '|st=10';
	}
	else{
		urlSave='main.asp?uri='+ curUrl + '&action=4&id=' +id;
		urlcll='|cll=1083|id=' + id + '|st=10';
		urlRtn='1083|id=' + id + '|st=10';
	}
	
	// RequestType allow add Document 
	if (bAddDoc=='True'){ 
		if(mdOpen('<center>Would you like to attach a document or file?</center>','300','140',' Yes ',' No ','','','','Request Attachments',''))
		{
			// If page is not secure	
			if (bSts!='True'){					
				document.frmManage.action=urlSave + goto +  DocPage + urlcll;				
				document.frmManage.submit();
				}
			else{
				frmManage.action=frmManage.action + goto + DocPage + urlcll;
				frmManage.submit();}	 
		}
		// Document is not attached
		else{		
			if (bSts!='True'){
				document.frmManage.action=urlSave + goto +  urlRtn;				
				document.frmManage.submit();
			}
			else{
				frmManage.action=frmManage.action + goto + urlRtn;
				frmManage.submit();
			}
		}	
		}
	else{			 // RequestType don't allow add Document 
			if (bSts!='True'){
				document.frmManage.action=urlSave + goto +  urlRtn + urlcll;
				document.frmManage.submit();
			}
			else{
				frmManage.action=frmManage.action + goto + urlRtn;
				frmManage.submit();
			}
	}	
}}

function ReqMBSubmit(id, bAddDoc, bSts, curUrl)
{	

if (valRequired()==''){	
	
	document.frmRQMBManage.hdnActive.value=1;
	var urlcll="";
	var urlSave="";
	var urlRtn="";
	var goto='&state=goto&goto=';
	var DocPage='1082|action=16|id=' + id;
	
	if (curUrl==2334){
		urlSave='admin.asp?uri='+ curUrl + '&action=4&id=' +id;
		urlcll='|cll=2334|action=1|id=' + id + '|st=10';
		urlRtn='2334|action=1|id=' + id + '|st=10';
	}
	else{
		urlSave='main.asp?uri='+ curUrl + '&action=4&id=' +id;
		urlcll='|cll=1083|id=' + id + '|st=10';
		urlRtn='1083|id=' + id + '|st=10';
	}
	
	// RequestType allow add Document 
	if (bAddDoc=='True'){ 
		if(mdOpen('<center>Would you like to attach a document or file?</center>','300','140',' Yes ',' No ','','','','Request Attachments',''))
		{
			// If page is not secure	
			if (bSts!='True'){					
				document.frmRQMBManage.action=urlSave + goto +  DocPage + urlcll;				
				document.frmRQMBManage.submit();
				}
			else{
				frmRQMBManage.action=frmRQMBManage.action + goto + DocPage + urlcll;
				frmRQMBManage.submit();}	 
		}
		// Document is not attached
		else{		
			if (bSts!='True'){
				document.frmRQMBManage.action=urlSave + goto +  urlRtn;				
				document.frmRQMBManage.submit();
			}
			else{
				frmRQMBManage.action=frmRQMBManage.action + goto + urlRtn;
				frmRQMBManage.submit();
			}
		}	
		}
	else{			 // RequestType don't allow add Document 
			if (bSts!='True'){
				document.frmRQMBManage.action=urlSave + goto +  urlRtn + urlcll;
				document.frmRQMBManage.submit();
			}
			else{
				frmRQMBManage.action=frmRQMBManage.action + goto + urlRtn;
				frmRQMBManage.submit();
			}
	}	
}}

function GoTo(id, Page, bSts){
	if (bSts!='True'){
		_onSave(document.frmManage,'2334&action=4&id=' + id + '&state=goto&goto=' + Page);
	}
	else{
		frmManage.action=frmManage.action+ '&state=goto&goto=' + Page;
		frmManage.submit();
	}
}


function cancelReq(cancelHREF){
	if(confirm('You are about to cancel the request')) document.location=cancelHREF;
}
function gotoPage(val){
	if (window.event.keyCode==13)	{ 
		var  url = '2334&action=1&st=2&id=' + val+ '&src=yes&state=goto&goto=';
		_onSave(document.frmManage,url);
	}
}

function LoadWOTypePage(ReqID, sGoToUrl, Secure) {
	
	var url="admin.asp?uri=7000&page=wotype";	

	WOType=window.showModalDialog(url, 'cal', 'dialogWidth:395 px;dialogHeight:200 px;resizable:yes;scroll:no;status:no;center:yes;unadorned:yes;help:no;');
	
	if (WOType!='' && typeof(WOType)!="undefined" && WOType!=0) {	
		if (ReqID>0)
		{
		GoTo(ReqID,sGoToUrl + '&wot=' + WOType, Secure);		
		}
	else
		{
		document.location='admin.asp?uri=2332&action=1&wot='+ WOType;
		}
	}	
	else
	{
	alert('Please, select WorkOrder type');
	return;
	}
}
function LoadServTypePage() {

	var url="admin.asp?uri=7000&page=srvtype";	
	
	WOType=window.showModalDialog(url, 'cal', 'dialogWidth:395 px;dialogHeight:200 px;resizable:yes;scroll:no;status:no;center:yes;unadorned:yes;help:no;');
	
	if (WOType!='' && typeof(WOType)!="undefined" && WOType!=0) {
		document.location='admin.asp?uri=2350&action=1&wot='+ WOType;
		
	}	
	else
	{
	alert('Please, select Service Contract type');
	return;
	}
}
function TypeSelect(){

     window.returnValue=document.frm.lstWOList.value;window.close();

}