/** @file scripts.js
 * @purpose contains javascript used within the cookbooks4sale site
 * @author Scott McDonald
 */

/** @function stripDollar
 * @purpose removes dollar signs from text input box
 * @author Scott McDonald
 * @param caller as form element
 * @return Void - Value is automatically assigned to input field
 */ function stripDollar($caller){$caller.value = vElem.replace(/\$/,"");}

/** @function addDefaultZero
 * @purpose inserts a "0" into an empty field
 * @author Scott McDonald
 * @param caller as form element
 * @return Void - autoassigned to field
 */ function addDefaultZero($caller){if($caller.value.length <= 0){$caller.value = 0}}

/** @function pVersion
 * @purpose creates the window for a printer friendly version of recipe of cookbook
 * @author Scott McDonald
 * @param query as String - for=recipe&id=42 || for=cookbook&id=42
 * @param idVal = recipe or cookbook is (in the above: 42)
 * @return void
 */ function pVersion(query,idVal){window.open('/print.php?'+query,idVal+'_print','height=500,width=650,menubar=yes,scrollbars=yes,resizable=yes,location=yes');}

/** @function emailFriend
 * @purpose opens email to friend window (disabled)
 * @author Scott McDonald
 * @param query as String - for=recipe&id=42 || for=cookbook&id=42
 * @param idVal = recipe or cookbook is (in the above: 42)
 * @return Void
 */ function emailFriend(query,idVal){window.open('/emailToFriend.php?'+query,idVal+'_print','height=500,width=650,menubar=yes,scrollbars=yes,resizable=yes,location=yes');}
	
/** @function addDate
 * @purpose given a day string, and a number of days, this will return a new date for use in the book creation form... 
 * @author Scott McDonald
 * @param date as String - date format should be yyyy-mm-dd
 * @param days as Number - numbe of days to add to the passed date
 * @return addDate Object(o.date=>DateObject, o.php=>yyyy-mm-dd, o.read=>mm-dd-yyyy)
 */ function addDate($strDate,$days){		
		$arrDate = $strDate.split("-");
		if($arrDate[0].length == 4){
			$year = $arrDate[0];
			$month = $arrDate[1]-1;
			$day = $arrDate[2];
		} else {
			$month = $arrDate[0]-1;
			$day = $arrDate[1];
			$year = $arrDate[2];
		}
			$date = new Date($year,$month,1*$day+$days);
			$rDate  = strPad(($date.getMonth()+1),2,0,1);
			$rDate += "-" + strPad($date.getDate(),2,0,1);
			$rDate += "-" + $date.getYear();
			$pDate = $date.getYear();		
			$pDate += "-" +  strPad(($date.getMonth()+1),2,0,1);
			$pDate += "-" + strPad($date.getDate(),2,0,1);

		this.obj = $date;
		this.php = $pDate;
		this.read = $rDate;
		return this;
	}
	
 /** @function selfListInternal_validate
 * @purpose validation for creating a self listing book when done by an internal account
 * @author Scott McDonald
 * @return Boolean - true if valid
 */ function selfListInternal_validate(){
		$form = document.selfListForm;
		var $return = "";
		if(!$form.self_insert_booknum.value){$return += "\n\t• Booknumber is empty.";}
		if(!$form.self_insert_email.value){$return += "\n\t• Email is empty.";}
		if(($form.self_insert_email.value)&&(!validEmail($form.self_insert_email.value))){$return += "\n\t• Invalid Email.";}
		if(!$form.self_insert_zip.value){$return += "\n\t• Zip Code is empty.";}
		if(($form.self_insert_zip.value) && (!isZip($form.self_insert_zip.value))) {$return += "\n\t• Invalid Zip Code.";}
		if($form.type.value == "add" && !$form.self_insert_password.value){$return += "\n\t• Password is empty.";}

		if($return!="") {alert(formerror + $return);}
		else {return(true);}
		return false;
	}

/** @function checkSIForm
 * @purpose checks all fields in a Self Listing Book submittion
 * @author Scott McDonald
 * @return Boolean : true if valid
 */ function checkSIForm(){
		$orderMethod = 0;
		$errorMessage = '';

		if($form.orgName.value==''){
			$errorMessage += "\t• Please enter an organization name.\t\n";
			markIt_Error("orgName","orgNameTitle","* Organization Name");
		}

		if($form.title.value==''){
			$errorMessage += "\t• Please enter your cookbook title.\t\n";
			markIt_Error("title","titleTitle","* Cookbook Title");
		}

		if($form.cbLocal.value==''){
			$errorMessage += "\t• Please indicate the city and state your organization is located.\t\n";
			markIt_Error("cbLocal","cbLocalTitle","* City, State");
		}

		if($form.recCount.value==''){
			$errorMessage += "\t• Please enter the number of recipes in your cookbook.\t\n";
			markIt_Error("recCount","recCountTitle","* Number of Recipes");
		}

		if($form.style.value=='xx'){
			$errorMessage += "\t• Please select the binding your cookbook has.\t\n";
			markIt_Error("style","styleTitle","* Binding Style");
		}

		if($form.cat1.value=='xx'){
			$errorMessage += "\t• Please select the first category you would like to list your cookbook under.\t\n";
			markIt_Error("cat1","cat1Title","* Category 1");
		}

		if($form.cat1.value=='99' && $form.cost.value>5){
			markIt_Error("cat1","cat1Title","* Category 1");		
			markIt_Error("cost","costTitle","* Book Cost");
		}

		if($form.cat2.value=='xx'){
			$errorMessage += "\t• Please select the second category you would like to list your cookbook under.\t\n";
			markIt_Error("cat2","cat2Title","* Category 2");
		}

		if($form.cat2.value=='99' && $form.cost.value>5){
			markIt_Error("cat2","cat2Title","* Category 2");		
			markIt_Error("cost","costTitle","* Book Cost");
		}

		if(($form.cat1.value=='99' || $form.cat2.value=='99') && $form.cost.value>5){
			$errorMessage += "\t• Books listed as Bargin can not cost more then $5.00.\t\n";
		}

		if($form.bookDesc.value==''){
			$errorMessage += "\t• Please enter a cookbook description.\t\n";
			markIt_Error("bookDesc","bookDescTitle","* Cookbook Description");
		}	

		if(($form.favRecName.value!="")||($form.favRecBy.value!="")||($form.favRecIng.value!="")||($form.favRecInst.value!="")){
			if($form.favRecName.value==""){
				$errorMessage += "\t• Enter a title for favorite recipe.\t\n";
				markIt_Error("favRecName","favRecNameTitle","* Recipe Title");
			}	

			if($form.favRecBy.value==""){
				$errorMessage += "\t• Enter a contributor name for favorite recipe.\t\n";
				markIt_Error("favRecBy","favRecByTitle","* Recipe Contributor");
			}	

			if(($form.favRecIng.value=="") && ($form.favRecInst.value=="")){
				$errorMessage += "\t• Enter favorite recipe ingredients.\t\n";
				markIt_Error("favRecIng","favRecIngTitle","* Recipe Ingredients");
			}	

			if($form.favRecInst.value==""){
				$errorMessage += "\t• Enter favorite recipe instructions.\t\n";
				markIt_Error("favRecInst","favRecInstTitle","* Recipe Instructions");
			}			
		}	

		if(($form.att.value!="")||($form.payTo.value!="")||($form.address.value!="")||($form.city.value!="")||($form.state.value!="XX")||($form.zip.value!="")){
			$orderMethod = 1;	
			if($form.att.value==""){
				$errorMessage += "\t• Enter a value for Attention/Care Of.\t\n";
				markIt_Error("att","attTitle","* Attention/Care Of");
			}
			if($form.payTo.value==""){
				$errorMessage += "\t• Enter a value for Make checks payable to.\t\n";
				markIt_Error("payTo","payToTitle","* Make Checks Payable to");
			}
			if($form.address.value==""){
				$errorMessage += "\t• Enter a value for mailing address.\t\n";
				markIt_Error("address","addressTitle","* Send Order To");
			}
			if($form.city.value==""){
				$errorMessage += "\t• Enter value for city.\t\n";
				markIt_Error("city","cityTitle","* City");
			}
			if($form.state.value=="XX"){
				$errorMessage += "\t• Select state.\t\n";
				markIt_Error("state","stateTitle","* State");
			}
			if($form.zip.value==""){
				$errorMessage += "\t• Enter a zip code.\t\n";
				markIt_Error("zip","zipTitle","* Zip");
			}
			if(($form.zip.value!='') && (!isZIP($form.zip.value))){
				$errorMessage += "\t• Please enter a valid zip code.\t\n";
				markIt_Error("zip","zipTitle","* Zip");
			}	
		}


		if(($form.phone.value)||($form.fax.value)){
			$orderMethod = 1;
			if(($form.phone.value) && (!validPhone($form.phone.value).st)){
				$errorMessage += "\t• Listed phone number is invalid. Please enter a valid phone number.\t\n";
				markIt_Error("phone","phoneTitle","* Phone");
			}
			if(($form.fax.value) && (!validPhone($form.fax.value).st)){
				$errorMessage += "\t• Listed fax number is invalid. Please enter a valid fax number.\t\n";
				markIt_Error("fax","faxTitle","* Fax");
			}
		}

		if(($form.email.value)||($form.url.value)){
			$orderMethod = 1;	
			if($form.email.value&&!validEmail($form.email.value)){
				$errorMessage += "\t• Listed email address is invalid. Please enter a valid email address.\t\n";
				markIt_Error("email","emailTitle","* E-mail address");
			}
			if($form.url.value&&!validUrl($form.url.value)){
				$errorMessage += "\t• Listed web address is invalid. Please enter a valid web address.\t\n";
				markIt_Error("url","urlTitle","* Web address");
			}
		}

		if($form.cost.value==''){
			$errorMessage += "\t• Please enter a price for your cookbook.\t\n";
			markIt_Error("cost","costTitle","* Book Cost");
		}
		if($form.shipping.value==''){
			$errorMessage += "\t• Please enter a shipping cost or \"0\" if none.\t\n";
			markIt_Error("shipping","shippingTitle","* Shipping & Handling");
		}

		if(!$orderMethod){
			$errorMessage += "\n\n\tYou must enter a way for your cookbook to be ordered.\n\tPlease enter Mailing, Phone or Electronic information.\n\tYou may fill in all 3 if you wish.\n";
			markIt_Error("mailOrderTable","mailOrderTableTitle","* Order By Mail");
			markIt_Error("phoneOrderTable","phoneOrderTableTitle","* Order By Phone");
			markIt_Error("emailOrderTable","emailOrderTableTitle","* Order Electronically");
		}

		if($errorMessage){
			$clientString = ($browser=="win:ie")?"\nFields marked in yellow are required.":"\nFields marked with an asterisk (*) are required";
			alert("Some changes are needed in order to submit this form.\n\n"+$errorMessage+$clientString);
			return false;
		} else {
			return true;
		}
	}
	
/** @function checkMailSet
 */ function checkMailSet(){
		if(
			($form.att.value!="") &&
			($form.payTo.value!="") &&
			($form.address.value!="") &&
			($form.city.value!="") &&
			($form.state.value!="XX") &&
			($form.zip.value!="")){
				markIt_Back("mailOrderTable","mailOrderTableTitle","* Order By Mail");
				markIt_Back("phoneOrderTable","phoneOrderTableTitle","* Order By Phone");
				markIt_Back("emailOrderTable","emailOrderTableTitle","* Order Electronically");
		}
	}

/** @function checkPhoneSet
 */ function checkPhoneSet(){
		if(($form.phone.value)||($form.fax.value)){
			markIt_Back("mailOrderTable","mailOrderTableTitle","* Order By Mail");
			markIt_Back("phoneOrderTable","phoneOrderTableTitle","* Order By Phone");
			markIt_Back("emailOrderTable","emailOrderTableTitle","* Order Electronically");
		}
	}
	
/** @function checkEmailSet
 */ function checkEmailSet(){
		if(($form.email.value)||($form.url.value)){
			markIt_Back("mailOrderTable","mailOrderTableTitle","* Order By Mail");
			markIt_Back("phoneOrderTable","phoneOrderTableTitle","* Order By Phone");
			markIt_Back("emailOrderTable","emailOrderTableTitle","* Order Electronically");
		}
	}

/** @function check_updateEmailAddressForm
 * @purpose ensures that the email field is not empty and is valid in the updateaccount form for selflisting
 * @author Scott McDonald
 * @param form as Form Object
 * @return Boolean true if Valid
 */ function check_updateEmailAddressForm($form){
		if($form.newEmail.value==''){
			alert('Please enter a value for new email.');
			return false;
		}
		if(!validEmail($form.newEmail.value)){
			alert('Please enter a valid e-mail address');
			return false;
		}
	}
	
/** @function validateNewPasswordForm
 * @purpose Ensures valid password entries in update account form for selflisting
 * @author Scott McDonald
 * @return Boolean - true if valid
 */ function validateNewPasswordForm(){
		$form = document.updatePasswordForm;
		if($form.currentPassword.value==''){
			alert("Please enter your current password.");
			return false;
		} else if($form.newPassword.value==''){
			alert("Please enter your new password.");
			return false;
		} else if($form.retypePassword.value==''){
			alert("Please retype your new password.");
			return false;
		} else if($form.retypePassword.value!=$form.newPassword.value){
			alert("Your new password must match in both fields.");
			return false;
		}
	}
	
/** @function loginFormCheck
 * @purpose ensures that all information is entered when logging into a self listing account
 * @author Scott McDonald
 * @return Boolean - true if valid
 */ function loginFormCheck(){
		$form = document.selflistLoginForm;
		$return = "";
		if($form.clientId.value == "") { $return += "\n• Book number not given"; }
		if($form.clientId.value != "" && ($form.clientId.value < 0 || $form.clientId.value > 1000000)) { $return += "\n• Book number invalid";}
		if($form.currentAccount[1].checked && $form.clientPassword.value == "") { $return += "\n• Password not provided"; }
		if(!$form.currentAccount[0].checked && !$form.currentAccount[1].checked) {$return += "\n• Please indicate if this is a new book";}
		if($return == "") {
			if($form.currentAccount[0].checked) { $form.act.value="newBook";}
			return true;
		}
		alert("This form could not be submitted as the following errors where found:\n" + $return);
		return false;
	}
	
	
/** @function validate_newbook
 * @purpose validates all information for a new self listing account form submittion
 * @author Scott McDonald
 * @return Boolean - true if valid
 */ function validate_newbook(){
			$form = document.newBookForm;
			$rString = "";
			if($form.clientId.value==''){
				$rString += "\t• No book number specified.\n";
			}
			if($form.clientId.value && $form.clientId.value.length<5){
				$rString += "\t• Book number must be 5 digits.\n";
			}
			if($form.clientzip.value==''){
				$rString += "\t• No zip code specified.\n";
			} else if(!isZIP($form.clientzip.value)){
				$rString += "\t• Zip code is invalid.\n";
			}
			if($form.clientEmail.value==''){
				$rString += "\t• No E-mail specified.\n";
			} else if(!validEmail($form.clientEmail.value)){
				$rString += "\t• E-mail address is invalid.\n";
			}
			if($form.clientPassword.value==''){
				$rString += "\t• No password specified.\n";
			} else if($form.clientRePassword.value==''){
				$rString += "\t• Password not retyped.\n";
			} else if($form.clientPassword.value!=$form.clientRePassword.value){
				$rString += "\t• Password and retyped password do not match.\n";
			}
			if($rString){
				if(mac&&ie){rtn=""} else {rtn="\n"}
				alert("This form could not be submitted at this time as errors have"+rtn+"been found. Please correct the following errors and submit"+rtn+"again.\n\n" + $rString + "\n");
				return false;
			} else {
				return true;
			}
		}