function popUp(URL) {
 day = new Date();
 id = day.getTime();
 eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=780,height=600,left=0,top=0');");
}

function trimString(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

function validateEmail(value) {
  
   var error=false;
   if(!isEmpty(value) && !isEmail(value)) {
   	error=true;
   }
  
   return !error;
}

function register(form)
{
       //alert("Our site is currently under construction,  please comeback later in a few weeks, Thank you!");
       //return false;
        
	var hasError = false;
	if (isEmpty(form.username.value)) {	
		alert("Email address is required.\nIt is only used as 'Username' for login and\nfor order confirmation by email.");
		form.username.focus();
		return false;
	} else {
		if(!isEmail(trimString(form.username.value))) {
			alert ("Invalid 'Email Address', please try again.");
			form.username.focus();
			return false;
		}
	}
	
	if (isEmpty(form.password_1.value)) {
			alert("'password' is a required field.");
			form.password_1.focus();
			return false;
			
	}
	if (isEmpty(form.password_2.value)) {
			alert("Please Re-type your password for security checking.");
			form.password_2.focus();
			return false;
	} else {
		if(form.password_2.value != form.password_1.value) {
			alert ("'password' does not match, please try it again.");
			form.password_2.focus();
			return false;
		}
	}
	/*
	if (isEmpty(form.user_email.value)) {
			alert("'user_email' is a required field.");
			form.user_email.focus();
			return false;
	} else {
		if(!isEmail(form.user_email.value)) {
			alert ("The Email address is invalid, please try again.");
			form.user_email.focus();
			return false;
		}
	}
	if (isEmpty(form.last_name.value)) {
			alert("'last_name' is a required field.");
			form.last_name.focus();
			return false;
	}
	if (isEmpty(form.first_name.value)) {
			alert("'first_name' is a required field.");
			form.first_name.focus();
			return false;
	}
	
	if (isEmpty(form.address_1.value)) {
			alert("'address_1' is a required field.");
			form.address_1.focus();
			return false;
	}
	if (isEmpty(form.city.value)) {
			alert("'city' is a required field.");
			form.city.focus();
			return false;
			
	}
	*/
	
	if (isEmpty(form.country.value)) {
		alert("'Country' is a required field.");
		form.country.focus();
		return false;
	} else {

	  if (form.country.value == "USA") {
	     if (isEmpty(form.state.value)) {
		alert("Please select a state from the state list.");
		form.state.focus();
		return false;
	      }

	  }
	  else {
	    if (isEmpty(form.region.value)) {
		alert("Please input a region/province.");
		form.region.focus();
		return false;
	    } else {
	    	form.state.value=form.region.value;
	    	//alert ("form.state.value=" + form.state.value);
	    }
	  }

		
	}
	
    	form.submit();
}

function validateSize(form, total)
{ 
	var numSize = 0;
//document.forms["formName"].elements["inpEl[]"][0]
	if (total > 1) { //validate array
		//alert("In total >1");
		for (var i=0; i < total; i++) {
		        if (numSize > 0) {
			   return true;
			}
			//num of Accept responses
			if (form.elements["products[]"][i].checked) {   	    	
			    numSize++;
			}
			
		} //end of for loop
	}//end of if (total > 1)
	else  { //validate non-array

		if (form.elements["products[]"].checked) {
			numSize++;
		}

	} //end of else
	
	if (numSize == 0) {
		return false;
	}
	return true;
}

function validateSize1(form)
{ 
	var numSize = 0;

	if (form.p1.checked || form.p2.checked || form.p3.checked || form.p4.checked || form.p5.checked || form.p6.checked
	    || form.p7.checked || form.p8.checked || form.p9.checked) {   	    	
	    numSize++;
	}
	
	if (numSize == 0) {
		return false;
	}
	return true;
}

function validateStudio(form, total)
{
	if (isEmpty(form.photo.value)) {	
	     alert("Please select a photo file (by 'Browse...' button), \nthen click 'Upload' button.");
	     return false;
	}
	//if (!validateSize1(form))	{
	//   alert("Please select portrait size(s), you can select multiple sizes if desired");
	//   return false;
	//}
	
	if (!validateSize(form, total))	{
	    alert("Please select a portrait size, you can select multiple sizes if desired");
	    return false;
	}
	if (confirm("Is the uploaded photo in accordance with the 'Photo Requirements' ?\n\n" + form.requirement.value + "\n\nClick 'OK' to continue; \nClick 'Cancel' to select a different 'Portrait Style' and/or photo."))
	{
           if (isEmpty(form.photo_desc.value)) {
           	if (confirm("Do you have any special comment for this portrait?" + "\nClick 'OK' to continue without any comment; \nClick 'Cancel' to add some special comments.")) {
           		form.submit();
           	} else {
           		form.photo_desc.focus();
           	}
           } else {
           	form.submit();
           }
           
        }
}

function updateShipStatus(form)
{
	
	if (form.order_status.value =='S' || form.order_status.value =='R') {
		if (isEmpty(form.shipment_tracking.value)) {
		
		alert("Please input shipment receipt information to 'Shipment Tracking' field.");
		form.shipment_tracking.focus();
		return false;
		}
	}
	if (confirm("Are you sure that you want to update the status for this order"))
    	       form.submit();
}


function submitShipAddr(form, type)
{
	var hasError = false;

	if (type == "S") {
	   if (isEmpty(form.address_type_name.value)) {
		
		alert("'Address Nickname' is a required field.");
		form.address_type_name.focus();
		return false;
	  }
	}
	
	if (isEmpty(form.first_name.value)) {
			alert("'First name' is a required field.");
			form.first_name.focus();
			return false;
			
	}
	if (isEmpty(form.last_name.value)) {
			alert("'Last Name' is a required field.");
			form.last_name.focus();
			return false;
	}
	if (isEmpty(form.address_1.value)) {
			alert("'Street Line 1' is a required field.");
			form.address_1.focus();
			return false;
	}
	if (isEmpty(form.city.value)) {
			alert("'City' is a required field.");
			form.city.focus();
			return false;
	}
	//if (isEmpty(form.state.value) && isEmpty(form.region.value)) {
	//		alert("'Region/Province' Or 'US State' are required. \nFor US location, please select a state from the list or \nInput a region/province for other location.");
	//		form.state.focus();
	//		return false;
	//}
	
	if (isEmpty(form.country.value)) {
			alert("'Country' is a required field.");
			form.country.focus();
			return false;
	} else {
	
	  if (form.country.value == "USA") {
	     if (isEmpty(form.state.value)) {
	  	alert("Please select a state from the 'US State' list or \nSelect a different country.");
		form.state.focus();
		return false;
	      }
	  
	  }
	  else {
	    if (isEmpty(form.region.value)) {
		alert("Please input a region/province.");
		form.region.focus();
		return false;
	    } else {
	    	form.state.value=form.region.value;
	    }
	  }
	  
	
	}
	if (isEmpty(form.zip.value)) {
			alert("'Zip Code' is a required field.");
			form.zip.focus();
			return false;
			
	}
	
	if (type == "U") {
	
	   if (!isEmpty(form.password_1.value) || !isEmpty(form.password_2.value)) {
	      if(form.password_2.value != form.password_1.value) {		
		alert ("'password' does not match, please try it again.");
		form.password_2.focus();
		return false;
	   }
		
	  }
	   if (isEmpty(form.user_email.value)) {
			
		alert("'Email Address' is a required field.");
		form.user_email.focus();
		return false;
	  }
	
	}

	if (type == "S" || type == "U" ) {
	   if (isEmpty(form.phone_1.value)) {
		
		alert("A 'Telephone' Number is a required for safer shipping and delivery. ");
		form.phone_1.focus();
		return false;
	  }
	}
    	form.submit();
}





function isEmpty(inputStr){
   if (inputStr == null || trimString(inputStr) == ""){
      return true;
   }

   return false;
}


function isNumeric(inputVal){

   oneDecimal = false;
   inputStr = inputVal.toString();
   for (var i = 0; i < inputStr.length; i ++){
      var oneChar = inputStr.charAt(i);
      if (i == 0 && oneChar == "-"){
         continue;
      }

      if (oneChar == "."){
         if (oneDecimal){
            return false;
         } else {
            oneDecimal = true;
         }
         continue;
      }

      if (oneChar < "0" || oneChar > "9"){
         return false;
      }
   }

   return true;
}


function isEmail(inputStr){
   if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(inputStr)){
     return true;
   }

   return false;
}


function isDate(yyyy,mm,dd){

   if(!isDigitsOnly(mm) || !isDigitsOnly(dd) || !isDigitsOnly(yyyy)){
      return false;
   }

   if (yyyy.length!=4) { // year should be 4 digits
      return false;
   }

   if (!(mm >= 1 && mm <= 12)) { // check month range
      return false;
   }

   if (!(dd >= 1 && dd <= 31)) { // check day range
      return false;
   }

   if ((mm == 4 || mm == 6 || mm == 9 || mm == 11) && dd==31) {
      return false;
   }

   if (mm == 2) { // check for february 29th
      var isleap = (yyyy % 4 == 0 && (yyyy % 100 != 0 || yyyy % 400 == 0));
      if (dd > 29 || (dd == 29 && !isleap)) {
         return false;
      }
   }

   return true;
}


function validateDate(dateField) {

    var strValue = dateField.value; //getObjectValue(dateField);
    //alert("Date is: " + strValue);
    strValue = strValue.replace( "-", "/");
    strValue = strValue.replace( "-", "/");
    if (document.all) strValue = strValue.replace( "\\", "/");
    else strValue = strValue.replace( /\\/gi, "/");
    if (document.all) strValue = strValue.replace( "\\", "/");
    else strValue = strValue.replace( /\\/gi, "/");
    //alert("Date is: " + strValue);
    var strArray = strValue.split("/");

    if (strArray.length <3)
    	return false;
    var mm = strArray[0];
    var dd = strArray[1];
    var yyyy = strArray[2];
    //alert("month: " + mm);
    //alert("year: " + yyyy);
    if (isEmpty(yyyy) || isEmpty(mm) || isEmpty(dd)){
         return false;
    }

    if(!isDate(yyyy,mm,dd)) {
        return false;
    }

   dateField.value = strValue;
   return true;
}


function checkAgreed(form)
{
	
	if (!form.chkAgreed.checked ) {
		
	alert("If you agree with the agreement, please check the \n'I agree with the agreement!' checkbox \nto activate your account");
	form.chkAgreed.focus();
	return false;
	}
	if (isEmpty(form.lastname.value)) {
	alert("'Last Name' is a required field.");
	form.lastname.focus();
	return false;
	} 
	
	if (isEmpty(form.firstname.value)) {
	alert("'First Name' is a required field.");
	form.firstname.focus();
	return false;
	} 
	
	if (isEmpty(form.company_name.value)) {
	alert("'Company Name' is a required field.");
	form.company_name.focus();
	return false;
	} 
	if (isEmpty(form.description.value)) {
	alert("'Description' is a required field.");
	form.description.focus();
	return false;
	} 
	
	if (isEmpty(form.email.value)) {
	alert("'E-mail' is a required field.");
	form.email.focus();
	return false;
	} 
	if (isEmpty(form.phone.value)) {
	alert("'Phone' is a required field.");
	form.phone.focus();
	return false;
	}
    	form.submit();
}

function validCatForm(form)
{
	
	if (isEmpty(form.category_name.value)) {
		alert("'Category Name' is a required field.");
		form.category_name.focus();
		return false;
	} 
	//if (isEmpty(form.category_description.value)) {
	//	alert("'Category Description' is a required field.");
	//	form.category_description.focus();
	//	return false;
	//} 
	if (isEmpty(form.ship_cost_1st_item.value) || !isNumeric(form.ship_cost_1st_item.value) ) {
		alert("'Cost For 1st Item' is a required 'numeric' field.");
		form.ship_cost_1st_item.focus();
		return false;
	}
	if (isEmpty(form.ship_cost_2nd_item.value) || !isNumeric(form.ship_cost_2nd_item.value) ) {
		alert("'Cost For Any Item After 1st' is a required 'numeric' field.");
		form.ship_cost_2nd_item.focus();
		return false;
	}
	
    	form.submit();
}

function validateQuote(form)
{
	if (isEmpty(form.photo.value)) {	
	     alert("Please select a picture/image file (by 'Browse...' button), \nthen click 'Upload' button.");
	     return false;
	}
        if (isEmpty(form.size_height.value)  
           || !isNumeric(form.size_height.value) ) {	
	     alert("Please specify the size in inch");
	     form.size_height.focus();
	     return false;
	}
       if (isEmpty(form.size_width.value) 
           || !isNumeric(form.size_width.value) ) {	
	     alert("Please specify the size in inch");
	     form.size_width.focus();
	     return false;
	}	
        if (isEmpty(form.my_price.value) || !isNumeric(form.my_price.value)) {	
	     alert("Please name your price in US $");
	     form.my_price.focus();
	     return false;
	}
	
       if (isEmpty(form.my_amount.value) || !isNumeric(form.my_amount.value) 
         || form.my_amount.value < 1 || (form.product_id.value > 0 && form.my_amount.value < 5 ) ) {	
	   
	   if (form.product_id.value < 0 ) {
	        alert("Please specify the number of items to purchase");
	    } else {
	     	alert("The number of items must be greater than 5 for bulk purchase");
	     }
	     
	     form.my_amount.focus();
	     return false;
	}
       	
       if (isEmpty(form.email.value) || !isEmail(form.email.value)) {	
	     alert("Please specify valid email address");
	     form.email.focus();
	     return false;
	}	
	
	
        if (isEmpty(form.photo_desc.value)) {
           if (confirm("Do you have any description/comment for this quote ?" + "\nClick 'OK' to continue without any description/comment; \nClick 'Cancel' to add some description/comments.")) {
           	form.submit();
           } else {
           	form.photo_desc.focus();
           }
       } else {
           form.submit();
       }
           
      
}

function validateQuestion(form)
{
       if (isEmpty(form.email.value) || !isEmail(form.email.value)) {	
	     alert("Please specify valid email address");
	     form.email.focus();
	     return false;
	}	
	
       if (isEmpty(form.question_desc.value)) {
           alert("Please Input Your Question Or Comment");
           form.question_desc.focus(); 
       } else {
          form.submit();
       }
           
      
}

function validateBestOffer(form)
{
        if (isEmpty(form.my_price.value) || !isNumeric(form.my_price.value)) {	
  	     alert("Please name your price in US $");
  	     form.my_price.focus();
  	     return false;
	}
	
       if (isEmpty(form.my_msg.value)) {
           alert("Please Input Your Message/Comment for Best Offer");
           form.my_msg.focus(); 
       } else {
          form.submit();
       }
           
      
}

function validateBrand(form)
{      
      //alert("hello" + new_func); 
      //form.brand_id.value
     //alert ("brand_id = " + form.brand_id.value);
     if (form.brand_id.value)
     	form.submit();  
     else 
     	alert ("Please make a valid selection");
}
