/*******************************************************************************
* Project: OmegaStage 1.0                                                      *
* File: omega.js                                                               *
* Author: Cody Garvin, Paul Levine                                             *
*                                                                              *
********************************************************************************
* Date: 11/02/05                                                               *
* Version: 0.1                                                                 *
* History:                                                                     *
*  11/02/05 - Initial script created.                                          *
*                                                                              *		
*******************************************************************************/
/* Included functions for repetitive tasks */

/////////////////////////////////////////////////////////////////////////////
// Refreshes the page
function refresh()
{
	//  false doesn't retrieve a document from the webserver... true does
	
	parent.location.reload( false );
}

/////////////////////////////////////////////////////////////////////////////
// Verifies a form (currently not used but here for reference)
function checkAccountForm()
{
		if (document.account.firstname.value == "")
		{
			alert("The First Name field is blank, Please fill it in.");
			document.account.firstname.focus();
			return false;
		}
		
		if (document.account.lastname.value == "")
		{
			alert("The Last Name field is blank, Please fill it in.");
			document.account.lastname.focus();
			return false;
		}
		
		// Made it through the jungle of if statements
		return true;
}

/////////////////////////////////////////////////////////////////////////////
// Test Email Format Validity
function isEmailAddress(emailAddress)
{
	return emailAddress.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
}
/////////////////////////////////////////////////////////////////////////////
// Check All Check boxes


var checkflag = "false";
function check(field) {
if (checkflag == "false") {
	for (i = 0; i < field.length; i++) {
		field[i].checked = true;}
		checkflag = "true";
		return false; 
} else {
	for (i = 0; i < field.length; i++) {
		field[i].checked = false; }
		checkflag = "false";
		return true; }
}


/////////////////////////////////////////////////////////////////////////////
// Toggles an element to show or hide
function toggle( targetId )
{ 
	if (document.getElementById)
	{ 
    	target = document.getElementById( targetId ); 
        
        if (target.style.display == "none")
        { 
        	target.style.display = ""; 
        } 
        else 
        { 
        	target.style.display = "none"; 
        } 
	} 
}

/////////////////////////////////////////////////////////////////////////////
// Tests the form to verify the required elements have been filled in
function requiredFields(formObj, fieldObj)
{
  	var fields = fieldObj.split(",");
  	var missing= new Array();
  	
  	for (var count = 0; count < fields.length; count++) 
  	{
		if (formObj.elements[fields[count]].value.length == 0) 
		{
	   		missing[missing.length] = new String(fields[count]);
		}
  	}
  	
  	if (missing.length) 
  	{
		alert("The field"+ ((missing.length > 1)?"s ":" ") + missing.join(", ") + " must be filled in to continue.");
		return false
  	}
  	
	return true;
}

/////////////////////////////////////////////////////////////////////////////
// Tests the form to verify the required elements have been filled in
function submitChangeStatus(targetId,myformnum)
{
	//document.form1.idchanged.value = targetId;
	myformnum.submit();
	window.location = 'changestatus.php?productid=' + targetId + '&status=' + statusChange;
}

/////////////////////////////////////////////////////////////////////////////
// Tests the form to verify the required elements have been filled in
function submitProductChange()
{
	
	document.productform.submit();
}
/////////////////////////////////////////////////////////////////////////////
// Test to make sure form has required fields filled in
var fieldstocheck = new Array();
    fieldnames = new Array();

function checkform() {
	

	
	if (document.getElementById("type").checked==false && document.getElementById("type1").checked==false) {
		alert("Please select a user type. If you are a software developer who wants to get your software tested, select 'Developer'. If you are a beta tester and would like to test the available software, select 'Beta Tester'.");
		return false;
	}
	
	if (!checkRequired()) {
	return false;
}
	
  if(! comparePasswords())
  {
    alert("The passwords you entered do not match. Re-enter them carefully and try again.");
    return false;
  }
  
if (document.getElementById('tos').checked == false) {
	alert("You must agree to our Terms of Service before you can continue");
	return false;
	}


	if (!checkPassword()) {
		alert("Your password must be between 5 and 15 characters long");
  		document.getElementById('pass').focus()
  		return false;
	}


/*check to make sure it is a valid email address
	if isEmailAddress("pspsps") {
		alert("ok");
		return true;
	} else {
		alert("The eamil address you have entered is not in the correct format. Please check to make sure you have entered it correctly and try again");
		return false;
	}
*/



	

  

  
  return true;
}

function checkRequired()
{
  for (i=0;i<fieldstocheck.length;i++) {
    if (eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].value") == "") {
      alert("Please enter your "+fieldnames[i]);
      document.subscribeform.elements[fieldstocheck[i]].style.background="#FFFD7A";
      eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].focus()");
      return false;
    } else {
    	document.subscribeform.elements[fieldstocheck[i]].style.background="white";
  }
  }
  return true;
}

function addFieldToCheck(value,name) {
  fieldstocheck[fieldstocheck.length] = value;
  fieldnames[fieldnames.length] = name;
}

function checkPassword()
{
	
var jpass=document.getElementById('pass').value

  if (jpass.length < 5) {
  	
  	return false;
  	
  } else {
  	return true;
  }
  

}




function comparePasswords()
{
  return (document.subscribeform.elements["pass"].value == document.subscribeform.elements["pass2"].value);
}


function compareEmail()
{
  return true;/*(document.subscribeform.elements["email"].value == document.subscribeform.elements["emailconfirm"].value);*/
}

////////////////////////////////////////////////////////////
// Ask to confirm an action
function askConfirm(msg)
{

var agree=confirm(msg);
if (agree) {
return true ;
} else {
return false ;
}
}

////////////////////////////////////////////////////////////////
// Validate Product Name
function validateProductName(fld) {
    var error = "";
    var illegalChars = /[^a-zA-z0-9_ ]/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = '#FFFD7A'; 
        error = "Please enter a product name.\n";
 		fld.focus();
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#FFFD7A'; 
        error = "Your product name can only contain letters, numbers, underscores and spaces.\n";
   
    } else {
        fld.style.background = 'White';
    } 
    if (error.length != 0) {
    		alert(error);
    		return false
    	
    } else
     {
     	
   return true
     }
    
}
