var CID;
var bidable;
var frmaction;

function fnSubmitChk(CID,bidable,frmaction)  {

function fnCheckFromSub()	{ 
	
	var Sub = document.getElementById("FromSub").value; 
	var State = document.getElementById("FromState").value; 
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)	{
	  alert ("Your browser does not support this feature.");
	  return;
	}        
	var url="asyncSubCheck.php";
	url=url+"?sub="+Sub;
	url=url+"&state="+State;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=function()	{
		if (xmlHttp.readyState==4)  {
		  if (xmlHttp.responseText!="valid")  {
		    alert("We couldn't find the pickup suburb you have entered. \nPlease check the suburb before trying again.");
		  }
		  else  {
		    fnCheckToSub();
		  }
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}

function fnCheckToSub()	{ 
	
	var Sub = document.getElementById("ToSub").value; 
	var State = document.getElementById("ToState").value; 
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)	{
	  alert ("Your browser does not support this feature.");
	  return;
	}        
	var url="asyncSubCheck.php";
	url=url+"?sub="+Sub;
	url=url+"&state="+State;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=function()	{
		if (xmlHttp.readyState==4)  {
		  if (xmlHttp.responseText!="valid")  {
		    alert("We couldn't find the delivery suburb you have entered. \nPlease check the suburb before trying again.");
		  }
		  else  {
		    fnSubmitQte();
		  }
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}

function fnSubmitQte()  {
  
  var intContinueYN = -1
  
  if (document.getElementById("hdnRestartWarning").value=="-1")  {
	var response = confirm("You have reached your ''Bidding Restart'' or ''Withdrawal Limit''. Withdrawing your job from bidding or making changes that will require the bidding process to restart will lock your job. It will be withdrawn from bidding permanently and all existing bids lost. \n\nAre you sure that you wish to continue?")
	if (!response)  {
	  return;
	}
	else  {
	  intContinueYN=0;
	}
  }
  if (CID=="close" & intContinueYN==-1)  {
	var response = confirm("This will save your details and WITHDRAW your job from the bidding proccess. All bids for this job will be lost and you will be required to start the bidding process later should you wish to proceed with this job.\n\nAre you sure that you wish to continue?")
	if (!response)  {
	  return;
	}
  }
  if (bidable==-1 & CID!="close" & intContinueYN==-1)  {
	var response = confirm("Your job is already open for bidding. If you have made changes that may effect pricing of your job may require bidding to restart and all previous bids may be lost.\n\nAre you sure that you wish to continue?")
	if (!response)  {
	  return;
	}
  }
  
  if(CID=="open")  {
    var FromDay = document.getElementById("FromDay").value;
    var FromMonth = document.getElementById("FromMonth").value;
    var FromYear = document.getElementById("FromYear").value;
    if (FromDay.length!=2 || FromMonth.length!=2 || FromYear.length!=4)  {
      alert("In order to open this job for bidding, you need to specify a valid \"Pickup Date\". Please check that you have entered a valid pickup date under the \"Job Requirements\" heading before trying again.");
      return false;
    }	
    var FromDate = new Date(FromYear,FromMonth,FromDay);
	//alert(FromDate);
	//return false;
  }
  
  if(document.getElementById("Password"))  {
    
	if(document.getElementById("Password").value!=document.getElementById("ConfirmPassword").value)  {
      alert("The \"Password\" you have entered does not match the \"Confirm Password\" you have entered. Please make sure they match before proceeding.");
	  document.getElementById("Password").focus();
	  return false;
    }
    
	if (fnRequiredField("EmailAddress","Email Address")==false)  {
	  return false;
	}
	
	if (fnRequiredField("Password","Password",6)==false)  {
	  return false;
	}
	
	if (fnRequiredField("ConfirmPassword","Password Confirmation",6)==false)  {
	  return false;
	}
	
	try  {
	  if(document.getElementById("EmailResponse").innerHTML=="Invalid Email")  {
	    alert("It appears that you have entered an invalid email address. Please check it before trying again.");
	    document.getElementById("EmailAddress").focus();
	    return false;
	  }
	}
	catch(err)  {
	}
	
  }
  
  var ItemCount = 0;
  var ElementID;
  var FieldVal;
  var inventArray = fnTrim(document.getElementById("FieldArray").value).split(" ");
  for (ElementID in inventArray)  {
	FieldVal = fnTrim(document.getElementById(inventArray[ElementID]).value);
	if(!isNaN(FieldVal) && FieldVal!="" )  {
	  ItemCount = ItemCount + parseInt(FieldVal);
	}
  }
  if (ItemCount==0)  {
    alert("It appears that you have not entered any inventory items. In order to save this job we require a list of items that need to be transported. Please fill out the inventory before continuing.");  
	return false;
  }
  
  document.getElementById("hdnCID").value=CID;
  
  if (document.getElementById("CapID"))  {
    fnSubmit(frmaction);
  }
  else  {
    document.getElementById("frmMain").action = frmaction;
    document.getElementById("frmMain").submit();
  }
  
}

fnCheckFromSub();

}

function fnIncrementItem(FieldRef)	{
	if (document.getElementById(FieldRef).value==""){
		document.getElementById(FieldRef).value=1;
	}
	else	{
	document.getElementById(FieldRef).value=parseInt(document.getElementById(FieldRef).value)+parseInt(+1)
	}
}

function fnDecrementItem(FieldRef)	{
	if (document.getElementById(FieldRef).value==""){
		document.getElementById(FieldRef).value="";
	}
	else	{
		if (document.getElementById(FieldRef).value==0){
			document.getElementById(FieldRef).value=0;
		}
		else	{
			document.getElementById(FieldRef).value=parseInt(document.getElementById(FieldRef).value)+parseInt(-1)
		}
	}
}
