/* --------------------------------------------------------------------------------
	File Name	: CommonScripts.js
	Date		: 11/10/2008
	Description : The file contains all the validation functions, dynacmic form
					display functions,delete confiramtion functions.
	----------------------------------------------------------------------------------*/

/* Generate a random image number from the image array and sets it to the main 
	image src attribute on the page. */
	
function ChangeImage()
{
	images = new Array;
	images[0] = "./Images/FrontPageRotation/1.jpg";
	images[1] = "./Images/FrontPageRotation/2.jpg";
	images[2] = "./Images/FrontPageRotation/3.jpg";
	images[3] = "./Images/FrontPageRotation/4.jpg";
	images[4] = "./Images/FrontPageRotation/5.jpg";
	images[5] = "./Images/FrontPageRotation/6.jpg";
	images[6] = "./Images/FrontPageRotation/7.jpg";
	images[7] = "./Images/FrontPageRotation/8.jpg";
	images[8] = "./Images/FrontPageRotation/9.jpg";
	images[9] = "./Images/FrontPageRotation/10.jpg";

	if(!(!(document.getElementById("mainImage")))){
		document.getElementById("mainImage").setAttribute("src", images[Math.floor(Math.random()*10)]);
	}
}
/* Validates all the forms present in the admin section and 
	display messages and sets hidden fields if any.*/

function ValidateForm(id,fieldsToValidate,IDFieldName,fieldsToHide)
{	
	if (IDFieldName != '')
		document.getElementById(IDFieldName).value = id;
	if(fieldsToHide != '')
	{
		var fieldsToHideArr = fieldsToHide.split(',');
		for(i=0;i<fieldsToHideArr.length;i++)
			document.getElementById(fieldsToHideArr[i]).style.cssText = 'display:none;';
	}
	if(fieldsToValidate != '')
	{
		var isError = 0;
		var formFieldsToValidate = fieldsToValidate.split(',');
		for(i=0;i<formFieldsToValidate.length;i++)
		{
			fieldsToValidateArr = formFieldsToValidate[i].split(':');
			if(fieldsToValidateArr[1] == 1)
			{
				validationSpan = fieldsToValidateArr[0] + 'Val'+ id;
				validateField = fieldsToValidateArr[0] + id;
				document.getElementById(validationSpan).style.cssText = 'display:none;';
				if(Trim(document.getElementById(validateField).value) == '' )
				{
					document.getElementById(validationSpan).style.cssText = 'display:true;';
					document.getElementById(validateField).focus();
					isError = 1;
				}
			}
			if(typeof(fieldsToValidateArr[2]) != 'undefined')
			{
				if(fieldsToValidateArr[2] == 'url')
				{
					if(!validateURL(fieldsToValidateArr[0],id))
						isError = 1;
				}
				else if(fieldsToValidateArr[2] == 'img')
				{
					if(!validateImageFormat(fieldsToValidateArr[0],id))
						isError = 1;
				}
			}
		}
	if(isError)
		return false;
	}
}
/* validate URL in form */
function validateURL(fieldToValidate,id)
{ 
	var urlExpression = /[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
	var validationField = fieldToValidate + id;
	var validationSpan = fieldToValidate + 'FormatVal' + id;
	var urlName = Trim(document.getElementById(validationField).value);
	document.getElementById(validationSpan).style.cssText = 'display:none';
	if(!urlName.match(urlExpression) && urlName != ''){
		document.getElementById(validationSpan).style.cssText = 'display:true';
		document.getElementById(validationField).focus();
		return false;
	}
	return true;
}

/* Validates the invalid image formats*/
function validateImageFormat(fieldToValidate,id)
{
	var validationField = fieldToValidate + id;
	var validationSpan = fieldToValidate + 'FormatVal' + id;
	var imgName = Trim(document.getElementById(validationField).value);
	var imgExpression = /(\.gif|\.jpg|\.jpeg|\.GIF|\.JPG|\.JPEG|\.bmp)$/;
	fld = document.getElementById(validationField);
	document.getElementById(validationSpan).style.cssText = 'display:none;';
	if(!imgName.match(imgExpression) && imgName != '')
	{
		document.getElementById(validationSpan).style.cssText = 'display:true;';
		fld.value = '';
		fld.focus();
		return false;
	}
	return true;
}

/*	Displays a confirmation while deleting any of the
	elements in admin section and sets hidden parameteres if any*/
function ConfirmDeletion(id,setID,type,submitform)
{
	if (setID != '')
		document.getElementById(setID).value = id;
	var deleteConf = confirm('Are you sure you want to delete this '+type+'? This procedure is irreversible.');	
	if(deleteConf)
		submitform + ".submit();"
	else
		return false;
}
/* Validates visitors information */
function ValidateVisitorInfo(name,email,phone)
{
	document.getElementById("phoneEmailValidation").style.cssText = 'display:none;';
	document.getElementById("nameValidation").style.cssText = 'display:none;';
	document.getElementById("emailValidation").style.cssText = 'display:none;';

	if(Trim(document.VisitorForm.visitorName.value) == '')
	{
		document.getElementById("nameValidation").style.cssText = 'display:true;';
		return false;
	}
	if(Trim(document.VisitorForm.email.value) == '' && Trim(document.VisitorForm.phone.value) == '')
	{
		document.getElementById("phoneEmailValidation").style.cssText = 'display:true;';
		return false;
	}
	if(Trim(document.VisitorForm.email.value) != '')
	{
		var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		str = document.VisitorForm.email.value;
		if(!str.match(emailRegEx))
		{
			document.getElementById("emailValidation").style.cssText = 'display:true;';
			return false;
		}
	}
	if((name != '' && name != Trim(document.VisitorForm.visitorName.value)) || (email != '' && email != Trim(document.VisitorForm.email.value)) || (phone != '' && phone != Trim(document.VisitorForm.phone.value)))
	{
		var deleteConf = confirm('The records will get updated. Are you sure you want to do that?');	
		if(deleteConf)
			return true;
		else
			return false;
	}
	return true;
}

/* Validates refer Friend form */
function validateFriendForm()
{
	document.getElementById("receiveremailCheckValidation").style.cssText = 'display:none';
	document.getElementById("receiveremailValidation").style.cssText = 'display:none';
	document.getElementById("senderNameValidation").style.cssText = 'display:none';
	if(Trim(document.referForm.receiverEmail.value) == '')
	{
		document.getElementById("receiveremailCheckValidation").style.cssText = 'dispaly:true';
		return false;
	}
	if(Trim(document.referForm.receiverEmail.value) != '')
	{
		var emailArr = '';
		var strMail = document.referForm.receiverEmail.value;
		var getValue = strMail.search(',');
		if(getValue != -1)
		 	strMail = strMail.replace(',',';');
		emailArr = strMail.split(';');
		var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		for(i=0;i<emailArr.length;i++)
		{
			if(!emailArr[i].match(emailRegEx))
			{
				document.getElementById("receiveremailValidation").style.cssText = 'display:true;';
				return false;
			}
		}
	}
	if(Trim(document.referForm.yourName.value) == '')
	{
		document.getElementById("senderNameValidation").style.cssText = 'display:true';
		return false;
	}
	return true;
}

/* Toggles the display of editing subcategory buttons and textbox in the inventory categories section.*/
function ToggleEditSubCategorySpans(subcatID,buttonType,subcatName)
{
	var childCatSaveCancel = 'childCatSaveCancel' + subcatID;
	var childCatEditDelete = 'childCatEditDelete' + subcatID;
	var childCatLabel = 'childCatLabel' + subcatID;
	var childCattxtBox = 'childCattxtBox' + subcatID;
	var editChildCatNameVal = 'editChildCatNameVal' + subcatID;
	var editChildCatName ='editChildCatName' + subcatID;
	if(buttonType == 'childCatEdit')
	{
		document.getElementById(childCattxtBox).style.cssText = 'display:true;';
		document.getElementById(editChildCatName).value = subcatName;
		document.getElementById(childCatLabel).style.cssText = 'display:none;';
		document.getElementById(childCatSaveCancel).style.cssText = 'display:true;';
		document.getElementById(childCatEditDelete).style.cssText = 'display:none;';
	}
	if(buttonType == 'childCatCancel')
	{
		document.getElementById(childCatSaveCancel).style.cssText = 'display:none;';
		document.getElementById(childCatEditDelete).style.cssText = 'display:true;';
		document.getElementById(editChildCatNameVal).style.cssText = 'display:none;';
		document.getElementById(childCattxtBox).style.cssText = 'display:none;';
		document.getElementById(childCatLabel).style.cssText = 'display:true;';
	}
}
/* Toggles the display of editing category buttons and textbox in the inventory categories section.*/
function ToggleEditCategorySpans(catID,buttonType,catName)
{
	var editCatName ='editCatName' + catID;
	var categoryEditMsg = 'categoryEditMsg' + catID;
	var subCategoryTable = 'subCategoryTable' + catID;
	
	
	if(buttonType == 'catEdit')	
	{
		eval("document.getElementById('categorytxtBox'+ catID).style.cssText = 'display:true'");
		document.getElementById(categoryEditMsg).style.cssText = 'width:300px;display:true;';
		document.getElementById(subCategoryTable).style.cssText = 'width:300px;display:none';		
		document.getElementById(editCatName).value = catName;
		eval("document.getElementById('categoryLabel'+catID).style.cssText = 'display:none'");
		eval("document.getElementById('saveCancel'+catID).style.cssText = 'display:true'");
		eval("document.getElementById('editDelete'+catID).style.cssText = 'display:none'");
	}
	if(buttonType == 'catCancel')
	{
		document.getElementById(subCategoryTable).style.cssText = 'width:300px;display:true;';
		document.getElementById(categoryEditMsg).style.cssText = 'width:300px;display:none;';
		eval("document.getElementById('editCatNameVal'+catID).style.cssText = 'display:none'");
		eval("document.getElementById('categorytxtBox'+catID).style.cssText = 'display:none'");
		eval("document.getElementById('categoryLabel'+catID).style.cssText = 'display:true'");
		eval("document.getElementById('saveCancel'+catID).style.cssText = 'display:none'");
		eval("document.getElementById('editDelete'+catID).style.cssText = 'display:true'");	
	}
}
/* Toggles the display of editing link buttons and textbox in the Link manager section.*/
function ToggleEditLinkForm(linkID,buttonType)
{
	var editUploadFailedVal = 'editUploadFailedVal'+linkID;
	if(buttonType == 'edit')
	{
		eval("document.getElementById('editDelete' + linkID).style.cssText = 'display:none;'");
		eval("document.getElementById('saveCancel' + linkID).style.cssText = 'display:true;'");
		eval("document.getElementById('editTable' + linkID).style.cssText = 'display:true;'");
		eval("document.getElementById('details' + linkID).style.cssText = 'display:none;'");		
		
	}
	if(buttonType == 'cancel')
	{
		document.editForm.reset();
		eval("document.getElementById('editDelete'+linkID).style.cssText = 'display:true'");
		eval("document.getElementById('saveCancel'+linkID).style.cssText = 'display:none'");
		eval("document.getElementById('editTitleVal'+linkID).style.cssText = 'display:none'");
		eval("document.getElementById('editUrlVal'+linkID).style.cssText = 'display:none'");
		eval("document.getElementById('editUrlFormatVal'+linkID).style.cssText = 'display:none'");
		eval("document.getElementById('editTable'+linkID).style.cssText = 'display:none'");
		eval("document.getElementById('details'+linkID).style.cssText = 'display:true'");
		eval("document.getElementById('editImgFormatVal'+linkID).style.cssText = 'display:none'");
	}
	
}
/* Toggles the display of editing item buttons and textbox in inventory item section.*/
function ToggleEditItemForm(itemID,buttonType)
{	
	if(buttonType == 'edit')
	{
		eval("document.getElementById('editDelete'+itemID).style.cssText = 'display:none'");
		eval("document.getElementById('saveCancel'+itemID).style.cssText = 'display:true'");
		eval("document.getElementById('editTable'+itemID).style.cssText = 'display:true'");
		eval("document.getElementById('details'+itemID).style.cssText = 'display:none'");
	}
	if(buttonType == 'cancel')
	{
		eval("document.getElementById('editForm'+itemID).reset()");
		eval("document.getElementById('editImgFormatVal'+itemID).style.cssText = 'display:none'");
		eval("document.getElementById('editDelete'+itemID).style.cssText = 'display:true'");
		eval("document.getElementById('saveCancel'+itemID).style.cssText = 'display:none;'");
		eval("document.getElementById('editTable'+itemID).style.cssText = 'display:none'");
		eval("document.getElementById('details'+itemID).style.cssText = 'display:true'");
		eval("document.getElementById('editInvNumVal'+itemID).style.cssText = 'display:none'");
	}
}

/* Returns a trimmed string */
function Trim(str)
{
	while(str.charAt(0) == (" ") )
	{
		str = str.substring(1);
	}
	while(str.charAt(str.length-1) == " " )
	{
		str = str.substring(0,str.length-1);
	}
	return str;
}

function openPopUp(itmpath)
{
	if(window.windowObj)
		windowObj.close();
	
	windowObj = window.open('ReferFriend.cfm?path='+itmpath, '', 'menubar=no, resizable=no,toolbar=no, height=350px, width=430px, screenx=40px, screeny=60px');
}