// following are the form hint function
/*
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function prepareInputsForHints() {
  var inputs = document.getElementsByTagName("input");
  for (var i=0; i<inputs.length; i++){
    if (inputs[i].parentNode.getElementsByTagName("span")[0]) {
      inputs[i].onfocus = function () {
        this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
      }
      inputs[i].onblur = function () {
        this.parentNode.getElementsByTagName("span")[0].style.display = "none";
      }
    }
  }
  var selects = document.getElementsByTagName("select");
  for (var k=0; k<selects.length; k++){
    if (selects[k].parentNode.getElementsByTagName("span")[0]) {
      selects[k].onfocus = function () {
        this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
      }
      selects[k].onblur = function () {
        this.parentNode.getElementsByTagName("span")[0].style.display = "none";
      }
    }
  }
}
addLoadEvent(prepareInputsForHints);
*/

//following are the change site name and choose colour functions
function changeSiteName(newName, clearField, whatElement, itSelf, hiddenInputValue) {
 if(clearField==true)
  document.getElementById(itSelf).value = "";

  document.getElementById(whatElement).innerHTML = newName; 
  document.getElementById(hiddenInputValue).value = newName;
}

// alert(testThis.match(illegalChars));

function checkIllChar(testThis){
 var illegalChars = /\W/;
 var stringAsArray = new Array();
 var replaceWith = "";
 for(i=0; i < testThis.value.length; i++){
  stringAsArray[i] = testThis.value.charAt(i);
  //alert(stringAsArray[i]);
 }

 if(illegalChars.test(testThis.value)) {
  for(i=0; i < stringAsArray.length; i++){
   if(stringAsArray[i].match(illegalChars)){
    stringAsArray[i] = "";
   }
   replaceWith += stringAsArray[i];
  }
  //alert(stringAsArray.length);
  alert('Please use allowed characters only.\nThe Illegal character(s) will be removed.');
  testThis.value=replaceWith;
  changeSiteName(replaceWith, false, 'site_name-url_change', 'site_name-id', 'site_name-hidden');
 }
}

/// begin: publication name as header function.
/// ------------------------------------------------------
/// check_file_type = value of the changed uploader field 
/// ------------------------------------------------------
function publicationHeader(ifClear, newValue){
 
 if(ifClear==true && document.getElementById('fileToUpload').value == "" || ifClear==true && document.getElementById('fileToUpload').value == null)
  document.getElementById('publication_name-id').value = "";
 
 if (document.getElementById('fileToUpload').value == "" || document.getElementById('fileToUpload').value == null) {
  if(document.getElementById('banner_image-hidden').value == "" || document.getElementById('banner_image-hidden').value == null){
   document.getElementById('upload_bar-text').innerHTML = newValue;
  }
  document.getElementById('live_site-banner').style.textAlign = "center";
  document.getElementById('upload_bar-text').style.fontSize = "3.5em";
  document.getElementById('publication_name-hidden').value = newValue;
 }
}





var changeElementsColour;
var storeColourValue;
var fillHex;
var isItTheText = false;

function chooseColour(whichElement, colourValueHolder, tellIfText, passHexField) {
 changeElementsColour = whichElement;
 storeColourValue = colourValueHolder;
 isItTheText = tellIfText;
 fillHex = passHexField;
 document.getElementById('colour-swatches_div').style.display = "block";
}

function chooseWhatColour(whatColour) {
 if(isItTheText == false){
  document.getElementById(changeElementsColour).style.backgroundColor = "#" + whatColour.substring(7,13);
  document.getElementById('live_site-footer_colour').style.backgroundColor = "#" + whatColour.substring(7,13);
  document.getElementById('live_site-banner').style.backgroundColor = "#" + whatColour.substring(7,13);
  document.getElementById(fillHex).value = whatColour.substring(7,13);
 }
 
 document.getElementById('colour-swatches_div').style.display = "none";
 document.getElementById(storeColourValue).value =  whatColour.substring(7,13);
 
 if(isItTheText == true){
  document.getElementById(changeElementsColour).style.color = "#" + whatColour.substring(7,13);
  document.getElementById('upload_bar-text').style.color = "#" + whatColour.substring(7,13);
  document.getElementById("sales_bar-text").innerHTML = "<b>Photo Sales</b>";
  document.getElementById(changeElementsColour).style.textAlign = "center";
  document.getElementById(fillHex).value = whatColour.substring(7,13);
 }
}

function closePalett() {
 setTimeout("document.getElementById('colour-swatches_div').style.display = 'none'", 250); 
}

function hexBannerColour(hexInputId, toClear, newHexValue, firstElementColourChange, secondElementColourChange){
 if(toClear==true)
  document.getElementById(hexInputId).value= "";
 
 if(toClear!=true) {
  document.getElementById(hexInputId).value = newHexValue;
  document.getElementById(firstElementColourChange).style.backgroundColor = "#" + newHexValue;
  document.getElementById("sales_bar-text").innerHTML = "Photo Sales";
  document.getElementById('live_site-banner').style.backgroundColor = "#" + newHexValue;
  document.getElementById(firstElementColourChange).style.textAlign = "center";
  document.getElementById(secondElementColourChange).style.backgroundColor = "#" + newHexValue;
 }
}

function hexTextColour(hexInputId, toClear, newHexValue, firstElementColourChange){
 if(toClear==true)
  document.getElementById(hexInputId).value= "";
 
 if(toClear!=true) {
  document.getElementById(hexInputId).value = newHexValue;
  document.getElementById(firstElementColourChange).style.color = "#" + newHexValue;
 }
}

/// begin: uploader functions.
/// ------------------------------------------------------
/// check_file_type = value of the changed uploader field 
/// ------------------------------------------------------
function ajaxFileUpload(check_file_type)
	{

/// ------------------------------------------------
/// 'fileExtention' 
/// returns the file extention of the uploaded file
/// ------------------------------------------------
	 var fileStart = check_file_type.lastIndexOf(".") + 1;
	 var fileEnd = check_file_type.length;
	 var fileExtention = check_file_type.substring(fileEnd,fileStart);

	 if (fileExtention == "jpg" || fileExtention == "JPG" || fileExtention == "Jpg" || fileExtention == "jpeg" || fileExtention == "JPEG" || fileExtention == "Jpeg" || fileExtention == "gif" || fileExtention == "GIF" || fileExtention == "Gif" ) {

		//starting setting some animation when the ajax starts and completes
		$("#upload_bar-image")
		.ajaxStart(function(){
			$(this).show();
		})
		.ajaxComplete(function(){
			$(this).hide();
		});
		
		/*
			prepareing ajax file upload
			url: the url of script file handling the uploaded files
            fileElementId: the file type of input element id and it will be the index of  $_FILES Array()
			dataType: it support json, xml
			secureuri:use secure protocol
			success: call back function when the ajax complete
			error: callback function when the ajax failed	
         */
		$.ajaxFileUpload
		(
			{
				url:'actions/doajaxfileupload.php',
				secureuri:false,
				fileElementId:'fileToUpload',
				dataType: 'json',
				success: function (data, status)
				{
					if(typeof(data.error) != 'undefined')
					{
						if(data.error != '')
						{
							//alert(data.error);
							$("#upload_bar-text").text(data.error);
						}else
						{
							var fileSizeStart = data.msg.lastIndexOf(('g' || 'G' || 'f' || 'F'), data.msg.length) + 1;
							var uploadFileSize = data.msg.substring(fileSizeStart);
							var uploadedImage = data.msg.replace(uploadFileSize, "");
							
							//alert(uploadFileSize);
							//alert(document.getElementById("maxFielSize").value);
							
							if(uploadFileSize > document.getElementById("maxFielSize").value){
								alert("Sorry, your file size is to big \n please upload an image less than 200KB");
							}else{
								$("#upload_bar-text").html("<img src='user_content/" + uploadedImage + "' class='img_valign' height='85'>");
								$("#banner_image-hidden").val(uploadedImage);
					    		$("#upload_bar-text").css({ backgroundColor:"white", textAlign:"center", padding:"0px", height:"85px"});
							}
						}
					}
				},
				error: function (data, status, e)
				{
					//alert(e);
					$("#upload_bar-text").text(e);
				}
			}
		)
		
		return false;


	} else {
	 alert("Please upload either a Jpg, Jpeg or Gif image file, thanks.");
	}
}


// ==============
// = reset form =
// ==============
function resetForms() {
   for (i=0; i < document.forms.length; i++) {
     document.forms[i].reset();
   }
}





/// begin: from validation function.
/// -----------------------------------------------------------
/// - Firstly the inital page load dom array variable is created
/// This has to be global to allow it to be checked by the next
/// function.
/// 
/// -Then the first function is created, this will trigger when the
/// page loads.
/// ------------------------------------------------------------

var initialDom = new Array();

function formValidationCheckOnLoad() {
	
resetForms();

/// -----------------------------------------------------------
/// - a local Array domArray is created to store the entire form
/// input tags.
///
/// - A whichNode variable is created to allow for an independent
/// incrementation value to be used by the global intitialDom 
/// Array which stores values sequentially and predictibaly 
/// into the array.
/// -----------------------------------------------------------
 var domArray = new Array();
 var whichNode = 0;
 domArray = document.getElementsByTagName("input");
 
/// -----------------------------------------------------------
/// - The for loop loops through the entire domArray array
/// 
/// - The if statement omitts the given conditional values
/// 
/// - The intialDom is then populated with the filtered 
/// domArray values sequentially and predictabaly
/// -----------------------------------------------------------
 for(i = 0; i < domArray.length; i++) {
  if(domArray[i].name != "" && domArray[i].type != "hidden" && domArray[i].type != "submit" && domArray[i].type != "file" && domArray[i].type != "radio") {
   initialDom[whichNode] = domArray[i].value;
   whichNode++;
  }
 }
 return initialDom;
}

/// -----------------------------------------------------------
/// - The next function is exicuted onfomrSubmit
/// Firstly a series of arrays/variables are created.
/// -----------------------------------------------------------
function formValidationCheckOnSubmit() {

/// -----------------------------------------------------------
/// - The errorArray will be filled with all the onFormSubmited
/// input form tags.
/// 
/// - The notFilledOnSubmit variable will hold the final 
/// not filled fileds values.
/// -----------------------------------------------------------
 var errorArray = new Array();
 var errorArray = document.getElementsByTagName("input");
 var notFilledOnSubmit = "Please complete the following:";

/// -----------------------------------------------------------
/// -The submittedDomValue will be populated with the filtered 
/// submitted form tag values and the submittedDomName with 
/// the names.
/// ----------------------------------------------------------- 
 var whichNode = 0;
 var submittedDomValue = new Array();
 var submittedDomName = new Array();
 
/// -----------------------------------------------------------
/// - For loop traverses the array, conditional if filters the
/// tags we don't use, submittedDomValue filled with tag values,
/// submittedDomNames filled with tag names
/// -----------------------------------------------------------
 for(i = 0; i < errorArray.length; i++) {
  if(errorArray[i].name != "" && errorArray[i].type != "hidden" && errorArray[i].type != "submit" && errorArray[i].type != "file" && errorArray[i].type != "radio") {
   submittedDomValue[whichNode] =  errorArray[i].value;
   submittedDomName[whichNode] = errorArray[i].alt;
   whichNode++;
  }
 }


/// -----------------------------------------------------------
/// - For loop traverses the array, conditional if filters the
/// values we don't use, then the notFilledOnSubmit variable
/// is populated with the string plus the submittedDomName values
///
/// -If the conditional statement is false, submit the form
/// ----------------------------------------------------------- 
 for(i = 0; i < submittedDomValue.length; i++) {
  if(submittedDomValue[i] == initialDom[i] || submittedDomValue[i] == "" || submittedDomValue[i] == null){
   notFilledOnSubmit +=  "\n\n \"" + submittedDomName[i] + "\"";
  }
 }

/// ----------------------------------------------------------- 
/// -If the conditional statement is true, show the 
/// notFilledOnSubmit valuse and don't submit the form by 
/// returning a false
/// ----------------------------------------------------------- 
 if (notFilledOnSubmit != null && notFilledOnSubmit != "" && notFilledOnSubmit != "Please complete the following:") {
  alert(notFilledOnSubmit);
  return false;
 }else{
  return true;
 }
}
/// ----------------------------------------------------------- 
/// - form check end
/// -----------------------------------------------------------


/// ----------------------------------------------------------- 
/// - terms function
/// -----------------------------------------------------------

// function aggreDisagree(agree, the_link) {
//  if(agree==true){
//   // document.pressphotosales_page07.submit();
//   window.location = the_link;
//  }else{
//   window.location = "http://www.prosnapfile.com" 
//  }
// }

// ========================
// = form price functions =
// ========================


// =================
// = checkMinPrice =
// =================
var mustSelectOne = new Array();
var checkIfSelected;
function checkMinPrice(getMin) {
	var re = new RegExp("[0-9]");
	var decim = new RegExp("\x46");
	var ill = /\W/;
	var illAlpha = new RegExp("[a-zA-Z]");
	if(getMin.value!=null){
		if(Math.round(getMin.value) < getMin.alt && Math.round(getMin.value) > 0){
			alert('minimum price is '+getMin.id);
			getMin.value = getMin.alt;
		}
		if(Math.round(getMin.value) > 10000){
			alert('maximum price is 10000');
			getMin.value = getMin.alt;
		}

		if(isNaN(getMin.value)){
			if(Math.round(getMin.value) == null || Math.round(getMin.value) == ""){
				return;
			}
			alert('Price has to be a number');
			getMin.value = getMin.alt;
		}
	}
}

var checkIfSelected = 0;
function storeSelection(turnOn, clicked) {
	if(clicked == true && checkIfSelected!= 0 && turnOn.value!= 0){
		checkIfSelected =- turnOn.value;
	}
	
	if(clicked == false){
		checkIfSelected =+ turnOn.value;
	}
	
	// alert(checkIfSelected);
}

function formPricingCheckOnSubmit(){
	// var priceFormArray = new Array();
	// priceFormArray = document.getElementsByTagName("input");
	// var addValues;
	// for(i=0; i < priceFormArray.length; i++) {
	// 	addValues += priceFormArray[i].value;
	// 	alert(priceFormArray[i].value);
	// }
	// if(addValues < 5) {
	// 	alert('At least one product must be created to continue');
	// 	return false;
	// }
	if (checkIfSelected <= 0) {
		alert('At least one product must be created to continue');
		return false;
	}else{
	 return true;
	}
}














