// JavaScript Document

//function openWin(url, name, width, height, left, top) {
//	myWindow = window.open(url, name, 'width='+width+',height='+height+',left='+left+',top='+top); 
//}


function openWin(url, name, width, height) {

	var left =(self.screen.width - width) / 2;
	var top =(self.screen.height -height) /2;

	myWindow = window.open(url, name, 'width='+width+',height='+height+',left='+left+',top='+top, resize='no'); 
}

function validateAddProductForm(form_name){

  var valid_form = true;
  var valid_msg = "Fields requiring attention:       \n\n";
  
  if(!trim(document.forms[form_name].name.value)){
    valid_form = false;
	valid_msg += "Name \n";
  }
  if(!trim(document.forms[form_name].quantity.value)){
    valid_form = false;
	valid_msg += "Quantity \n";
  }
  if(isNaN(document.forms[form_name].quantity.value)){
	valid_form = false;
	valid_msg += 'Quantity must have only numbers. \n';
  }
  if(!trim(document.forms[form_name].blurb1.value)){
    valid_form = false;
	valid_msg += "Blurb 1 \n";
  }
  if(!trim(document.forms[form_name].description.value)){
    valid_form = false;
	valid_msg += "Description \n";
  }
  /*
  if(!trim(document.forms[form_name].repassword.value)){
    valid_form = false;
	valid_msg += "Confirmation Password    \n";
  }
  if(document.forms[form_name].repassword.value !== document.forms[form_name].password.value){
    valid_form = false;
	valid_msg += "Passwords don't match    \n";
  }
*/

  if(valid_form){
    //window.alert(valid_form);
    document.forms[form_name].submit();
  }else{
  //document.forms.signup.submit();
    window.alert(valid_msg);
  }

}




function validateEditProductForm(form_name){

  var valid_form = true;
  var valid_msg = "Fields requiring attention:       \n\n";
  
  if(!trim(document.forms[form_name].name.value)){
    valid_form = false;
	valid_msg += "Name \n";
  }
  if(!trim(document.forms[form_name].quantity.value)){
    valid_form = false;
	valid_msg += "Quantity \n";
  }
  if(isNaN(document.forms[form_name].quantity.value)){
	valid_form = false;
	valid_msg += 'Quantity must have only numbers. \n';
  }
  if(!trim(document.forms[form_name].blurb1.value)){
    valid_form = false;
	valid_msg += "Blurb 1 \n";
  }
  if(!trim(document.forms[form_name].description.value)){
    valid_form = false;
	valid_msg += "Description \n";
  }
  /*
  if(!trim(document.forms[form_name].repassword.value)){
    valid_form = false;
	valid_msg += "Confirmation Password    \n";
  }
  if(document.forms[form_name].repassword.value !== document.forms[form_name].password.value){
    valid_form = false;
	valid_msg += "Passwords don't match    \n";
  }
*/

  if(valid_form){
    //window.alert(valid_form);
    document.forms[form_name].submit();
  }else{
  //document.forms.signup.submit();
    window.alert(valid_msg);
  }

}


function validateUserEditForm(form_name){

  var valid_form = true;
  var valid_msg = "Fields requiring a value:       \n\n";

    if(!trim(document.forms[form_name].password.value)){
    valid_form = false;
	valid_msg += "Password     \n";
  }
  if(!trim(document.forms[form_name].repassword.value)){
    valid_form = false;
	valid_msg += "Confirmation Password    \n";
  }
  if(document.forms[form_name].repassword.value !== document.forms[form_name].password.value){
    valid_form = false;
	valid_msg += "Passwords don't match    \n";
  }

  if(valid_form){
    //window.alert(valid_form);
    document.forms[form_name].submit();
  }else{
  //document.forms.signup.submit();
    window.alert(valid_msg);
  }

}

/*
function validatePasswordForm(){
  if(validateEmail(trim(document.password.email.value),1,1)){
    document.password.submit();
  }
}  
*/


function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function



function validateEmail(addr,man,db) {
	if (addr == '' && man) {
	   //if (db) window.alert('Email address is mandatory');
	   return false;
	}
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
	   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
		  if (db) window.alert('Email address contains invalid characters');
		  return false;
	   }
	}
	for (i=0; i<addr.length; i++) {
	   if (addr.charCodeAt(i)>127) {
		  if (db) window.alert("Email address contains non ascii characters.");
		  return false;
	   }
	}
	
	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
	   if (db) window.alert('Email address must contain an @');
	   return false;
	}
	if (atPos == 0) {
	   if (db) window.alert('Email address must not start with @');
	   return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
	   if (db) window.alert('Email address must contain only one @');
	   return false;
	}
	if (addr.indexOf('.', atPos) == -1) {
	   if (db) window.alert('Email address must contain a period in the domain name');
	   return false;
	}
	if (addr.indexOf('@.',0) != -1) {
	   if (db) window.alert('Period must not immediately follow @ in email address');
	   return false;
	}
	if (addr.indexOf('.@',0) != -1){
	   if (db) window.alert('Period must not immediately precede @ in email address');
	   return false;
	}
	if (addr.indexOf('..',0) != -1) {
	   if (db) window.alert('Two periods must not be adjacent in email address');
	   return false;
	}
	//var suffix = addr.substring(addr.lastIndexOf('.')+1);
	//if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
	//   if (db) alert('invalid primary domain in email address');
	//   return false;
	//}
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	if (suffix.length < 2 ) {
	  if (db) window.alert('Invalid primary domain in email address');
	  return false;
	}
	return true;
}

