// JavaScript Document
// This file is called from the apply.asp Module.
// This verifies if the user has provided all the correct data before it can be saved 
// Date Written July 28, 2008
// Author: Mahmood Mir

function validateSignUp() {
	var errMsg = "";		// Create a blank error message variable
	
	// Gather form data in local variables to check values
	var elForm = document.sqaSignUp;						// The form in the document
	var vCName = elForm.txContact.value;
	var vFirm  = elForm.txFacility.value;
	var vAddress = elForm.txStreet.value;
	var vCity = elForm.txCity.value;
	var vZIP = elForm.txZIP.value;
	var vPhone = elForm.txPhone.value;
	var vFax = elForm.txFax.value;
	var vEmail = elForm.txEmail.value;
	var vPass = elForm.txPass.value;
	var vPass2 = elForm.txVerify.value;
	
// Start the checking process and store the error messages.
	if (vCName.length < 2) errMsg += "- enter Contact Name, Administrator or Licensee name. \n";
	if (vFirm.length < 4) errMsg += "- mention your Facility name, if none then enter \"Independant\" \n";
	if (vAddress.length < 5) errMsg += "- provide your complete ADDRESS, Street Name and Number. \n";
	if (vCity.length < 4) errMsg += "- complete your address by providing the CITY Name. \n";
	if (vPhone.length < 10) errMsg += "- enter phone number including your area code. \n";
	if (vZIP.length < 5) errMsg += "- enter the ZIP Code for complete contact Address. \n";
	if (vEmail.length < 5 || vEmail.indexOf(' ',0) > 0 || vEmail.indexOf('@',0) < 1 || vEmail.indexOf('.',0) < 1)
		errMsg += "- state your correct EMAIL address for verification and login purposes. \n";
	if (vPass.length < 6 || vPass != vPass2) errMsg += "- password must be at least 6 characters and it must be verified.";
	
	errPlease = "Sorry! Your request cannot be process. Please: \n\n";
	if (errMsg.length > 0) {
		errMsg = errPlease + errMsg;
		alert(errMsg);
		return false;
	}
	else {
		elForm.action = "doApply.asp";
		elForm.submit();
	}
}

function validateLogIn() {
	var errMsg = "";		// Create a blank error message variable
	var elForm = document.sqaLogin;						// The form in the document

	var vEmail = elForm.txLogin.value;
	var vPass = elForm.txPWord.value;
	
	if (vEmail.length < 5 || vEmail.indexOf(' ',0) > 0 || vEmail.indexOf('@',0) < 1 || vEmail.indexOf('.',0) < 1)
		errMsg += "- Your login is your email address that you provided. \n";
	if (vPass.length < 6) errMsg += "- password should be at least 6 characters.";

	errPlease = "Sorry! Login won't be possible. Please: \n\n";
	if (errMsg.length > 0) {
		errMsg = errPlease + errMsg;
		alert(errMsg);
		return false;
	}
	else {
		elForm.action = "doLogin.asp";
		elForm.submit();
	}

}

function doBuy() {
	var errMsg = "";		// Create a blank error message variable
	
	var elForm = document.buyForm;						// The form in the document
	var buyQty = elForm.txQty.value;
	
	if(isNaN(buyQty)) errMsg += "- Quantity must be a digit.\n";
	if(buyQty <= 0) errMsg += "- Must be a positive number, greater than zero.\n";
	if(buyQty.indexOf(".") >= 0) errMsg += "Must be a whole number, no decimals. \n";

	errPlease = "Sorry! Transaction won't be possible. Please: \n\n";
	if (errMsg.length > 0) {
		errMsg = errPlease + errMsg;
		alert(errMsg);
		return false;
	}
	else {
		elForm.action = "doPurchase.asp";
		elForm.submit();
	}

}

function goPrev(pos) {
	goForm(pos,pos-1);
}

function goNext(pos) {
	goForm(pos,pos+1);
}



function goForm(step, dir) {
  //qString = new Querystring()
  //fNum = qString.get("fn");
  //fKey = qString.get("ak");
  //lPage = "doReport.asp?sf=" + step + "&dr=" + dir + "&fn=" + fNum + "&ak=" + fKey;
  
  //Checking for valid date in the last section "Strength Form"
  if(step==7) {
   errMsg = "";
   repDate = document.cForm.txDate.value;
   repName = document.cForm.txReporter.value;
   repFLoc = document.cForm.txFacility.value;
   
   if(!isValidDate(repDate)) errMsg += "- Invalid Date format, or date not provided.\n";
   if(repName.length < 2) errMsg += "- Must have the person's name completing this report.\n";
   if(repFLoc.length < 2) errMsg += "- Need the facility Name to complete the report.\n";
   
	errPlease = "Sorry! Cannot proceed, because: \n\n";
	if (errMsg.length > 0) {
		errMsg = errPlease + errMsg;
		alert(errMsg);
		return false;
    }
  }
  thisForm = document.cForm;

  thisForm.sf.value = step;
  thisForm.dr.value = dir;
//  alert(step + " and " + dir);
  thisForm.action = "doReport.asp";
  thisForm.submit();
}

function loadForm(step,dir,fnum,fkey) {
 	thisForm = document.cForm;
	thisForm.fn.value = fnum;
	thisForm.ak.value = fkey;
//	alert(fnum + " and " + fkey);
	goForm(step, dir);
}

// Library Function
/* Client-side access to querystring name=value pairs
	Version 1.3
	28 May 2008
	
	License (Simplified BSD):
	http://adamv.com/dev/javascript/qslicense.txt
*/
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = {};
	
	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);
		
		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;
		
		this.params[name] = value;
	}
}

Querystring.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}



//Function to validate the Date input
/*Original:  Sandeep V. Tamhankar (stamhankar@hotmail.com)

This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com */

//Begin
function isValidDate(dateStr) {
// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
// Also separates date into month, day, and year variables

//var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

// To require a 4 digit year entry, use this line instead:
 var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

 var matchArray = dateStr.match(datePat); // is the format ok?
 if (matchArray == null) {
  //alert("Date is not in a valid format.")
  return false;
 }

 month = matchArray[1]; // parse date into variables
 day = matchArray[3];
 year = matchArray[4];
 if (month < 1 || month > 12) { // check month range
  //alert("Month must be between 1 and 12.");
  return false;
 }

 if (day < 1 || day > 31) {
  //alert("Day must be between 1 and 31.");
  return false;
 }

 if ((month==4 || month==6 || month==9 || month==11) && day==31) {
  //alert("Month "+month+" doesn't have 31 days!")
  return false
 }

 if (month == 2) { // check for february 29th
  var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
  if (day>29 || (day==29 && !isleap)) {
   //alert("February " + year + " doesn't have " + day + " days!");
   return false;
  }
 }
 return true;  // date is valid
}
