function addRemoveMyList(objCheckBox, famId){	
	formElement = document.getElementById(objCheckBox);
	formElement.checked ? addToMyList(formElement.value, famId): removeFromMyList(formElement.value, famId);
}

function checkLink(objCheckBox, famId){	
	formElement = document.getElementById(objCheckBox);
	formElement.checked ? formElement.checked=false: formElement.checked=true;
	formElement.checked ? addToMyList(formElement.value, famId): removeFromMyList(formElement.value, famId);
}



function addToMyList(checkBoxValue, famId)
{	
	var i = 0;
	var strNewCookie = "";
	var cookieName = "myList"+ famId;
	//alert(cookieName);
	var strCurrentCookie = getcookie(cookieName);
	var aCurCookiesInfo = strCurrentCookie.split(",");
	while(i < aCurCookiesInfo.length) {
			if (aCurCookiesInfo[i] != checkBoxValue) {
				if (aCurCookiesInfo[i].length > 0) {
					strNewCookie += aCurCookiesInfo[i] + ",";
				}
			}
			i++;
		}
		strNewCookie += checkBoxValue;
		//alert(strNewCookie);
		document.cookie = cookieName+"="+strNewCookie+"; expires=Tue, 3 Aug 2010 20:47:11 UTC; path=/";
}

function removeFromMyList(checkBoxValue, famId)
{	
	//alert(checkBoxValue);
	var i = 0;
	var strNewCookie = "";
	var cookieName = "myList"+ famId;
	var strCurrentCookie = getcookie(cookieName);
	if (strCurrentCookie.match(','+checkBoxValue)){
			strNewCookie = strCurrentCookie.replace(','+checkBoxValue,"");
		} else if (strCurrentCookie.match(checkBoxValue)) {
			strNewCookie = strCurrentCookie.replace(checkBoxValue,"");
		}else {
		strNewCookie= strCurrentCookie;
		}
	//alert(strNewCookie);
	document.cookie = cookieName+"="+strNewCookie+"; expires=Tue, 3 Aug 2010 20:47:11 UTC; path=/";
}


function checkMyList(objCheckBox, famId) {
	
	var i = 0;
	var cookieName = "myList"+ famId;
	var myListCookie = getcookie(cookieName);
	//alert(cookieName);
	var cookieArray = myListCookie.split(",");
	for(i=0;i<objCheckBox.length;i++) {
		for(j=0; j<cookieArray.length; j++){
			if (objCheckBox[i][1] == cookieArray[j]) {
			document.getElementById(objCheckBox[i][0]).checked = true;
				}
		}
	}
}

function getcookie(cookiename) {
 var cookiestring=""+document.cookie;
 var index1=cookiestring.indexOf(cookiename);
 if (index1==-1 || cookiename=="") return ""; 
 var index2=cookiestring.indexOf(';',index1);
 if (index2==-1) index2=cookiestring.length; 
 return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}

// These are the My List Functions

function updateMyList() {
	var strCookie = "";
	for(i=0;i<document.myList.myListId.length;i++) {
		if (!document.myList.myListId[i].checked) {
			strCookie = strCookie + document.myList.myListId[i].value + ",";
		}
	}
	if (strCookie.charAt(strCookie.length -1) == ",") {
		strCookie = strCookie.substring(0,strCookie.length -1);
	}
	document.cookie = "opecMyList=" + strCookie + "; expires=Tue, 3 Aug 2010 20:47:11 UTC; path=/";
	self.location.reload(true);
}

function clearMyList() {
	if (confirm ("Are you sure you want to clear your entire list?")) {
		document.cookie = "opecMyList=; expires=Tue, 3 Aug 2010 20:47:11 UTC; path=/";
		self.location.reload(true);
	} else {
		return false;
	}
}

function printMyList() {
	alert('print functionality to be added when page information finalised');
}

function emailMyList(strWhere) {
	document.myList.emailtype.value = strWhere;
	if (strWhere == "friend") {
		document.myList.action = "../getFriendEmail.asp";
	}
	document.myList.submit();
}

function addNewItem(strType) {
	var strURL = "edit" + strType + "item.asp?id=new";
	window.open(strURL, "EditWindow", 'toolbar=no,width=800,height=600,resize=yes,scrollbars=1') 
}

function openEditItem(strType, intID, intSCID, intCID, intFID) {
	var strURL = "edit" + strType + "item.asp?id=" + intID + "&scid=" + intSCID + "&cid=" + intCID + "&fid=" + intFID;
	window.open(strURL, "EditWindow", 'toolbar=no,width=800,height=600,resize=yes,scrollbars=1') 
}

function openEditAttribute(intID) {
	var strURL = "editAttribute.asp?id=" + intID;
	window.open(strURL, "EditAttributeWindow", 'toolbar=no,width=800,height=450,resize=yes,scrollbars=1') 
}

function validateForm(objForm, strFormType) {
	if (strFormType != 'product') {
		if (objForm.title.value.length > 0) {
			return true;
		}else {
			alert("Please add a title");
			objForm.title.focus();
			return false;
		}
	} else {
		if (objForm.description.value.length > 0) {
			return true;
		}else {
			alert("Please add a Description");
			objForm.description.focus();
			return false;
		}
	}
}

function addNewAttribute(objForm) {
	if (objForm.attribute_id.value > 0) {
		var strAttributeValue = prompt("Please Enter a value for this Attribute:");
		objForm.attribute_value.value = strAttributeValue;
		objForm.formFunction.value = "addNew";
		objForm.action = "submitAttribute.asp"
		objForm.submit();
	}else{
		alert("Please select an attribute");
		objForm.attribute_id.focus();
	}
}

function editAttribute(objForm, strCurrentValue, intAttributeID) {
	var strAttributeValue = prompt("Please Modify the value for this Attribute:", strCurrentValue);
	if (strAttributeValue == null || strAttributeValue == "" || strAttributeValue == " ")
	{
		strAttributeValue = strCurrentValue;
	}
	objForm.attribute_value.value = strAttributeValue;
	objForm.attribute_id_hidden.value = intAttributeID;
	objForm.formFunction.value = "editCurrent";
	objForm.action = "submitAttribute.asp"
	objForm.submit();
}

function addNewAttributeName(objForm) {
	var strAttributeValue = prompt("Please Enter a Name for this Attribute:");
	objForm.attribute_name.value = strAttributeValue;
	objForm.formFunction.value = "addNew";
	objForm.action = "submitAttributeName.asp"
	objForm.submit();
}

function editAttributeName(objForm, strCurrentName, intAttributeID) {
	var strAttributeName = prompt("Please Modify the Name for this Attribute:", strCurrentName);
	if (strAttributeName == null || strAttributeName == "" || strAttributeName == " ")
	{
		strAttributeName = strCurrentName;
	}
	objForm.attribute_name.value = strAttributeName;
	objForm.attribute_id_hidden.value = intAttributeID;
	objForm.formFunction.value = "editCurrent";
	objForm.action = "submitAttributeName.asp"
	objForm.submit();
}

function SetUrl(strURL, strWhat) {
	eval("document.frm1."+strWhat+".value=strURL");
	eval("document."+strWhat+"_image.src=strURL");
}

function openContent(strURL) {
	window.open(strURL, "contentWindow", "height=500,width=800,scrollbars=yes");
}
function openSmallWindow(strURL) {
	window.open(strURL, "contentWindow", "height=300,width=280,scrollbars=yes");
}
