/*********************************************************************/
/* File Name:  publicFunctions.js                                    */
/*             all general functions related to Gold Line projects   */
/* Company:    Group of Gold Line                                    */
/* Scriptor:   Behzad Talebpour                                      */
/*********************************************************************/

/* this adds the created "select" and "clear" buttons, returned from other functions,
/* to the given field.
/*/
function addButtons(side, daCode, fldName, fldVal, pos, defCat){
   var btnSel = createSelectButton(side, daCode, fldName, fldVal, defCat);
   var btnClr = createClearButton(fldName);
   getDivElement(pos).innerHTML += btnSel + btnClr;
   //getDivElement(pos).innerHTML += '<p style="width:'+FLD_WIDTH+'">'+ 
   //   btnSel+btnClr+'</p>';
}

/* this adds the created "select" and "clear" buttons, returned from other functions,
/* to a specific field which is fixed and added manually.
/*/
function addButtons2Virtual(side, daCode, fldName, fldVal, pos, defCat){
   var btnSel = createSelectButton(side, daCode, fldName, fldVal, defCat);
   var btnClr = createClearButton(fldName);
   
   //document.getElementById(pos).innerHTML +=
   //   '<p style="width:'+FLD_WIDTH+'">'+btnSel+btnClr+'</p>';
   document.getElementById(pos).innerHTML += btnSel + btnClr;
}

function changeAttribs(fldName){
   changeBackground(fldName);
   changeWidth(fldName);
}

function changeBackground(fldName, clr){
   if (changeBackground.arguments.length == 1)
      var c = BG_COLOR;
   else
      var c = clr;
      
   getFieldElement(fldName).style.backgroundColor = c;
}

function changeCardType(){
   refreshPage();
}

function changeImgAttribs(fldName, clr, widthVal){
   var c, w;
   switch (changeImgAttribs.arguments.length){
      case 1:
         c = BG_COLOR;
         w = FLD_WIDTH;
         break;
      case 2:
         c = clr;
         w = FLD_WIDTH;
         break;
      case 3:
         c = clr;
         w = widthVal;
   }
   document.getElementById("FIELD_" + FieldIDs[fldName] +
      "_IMGNAME").style.backgroundColor = c;
   document.getElementById("FIELD_" + FieldIDs[fldName] +
      "_IMGNAME").style.width = w;
}

function changeWidth(fldName, widthVal){
   if (changeWidth.arguments.length == 1)
      var w = FLD_WIDTH;
   else
      var w = widthVal;

   getFieldElement(fldName).style.width = w;
}

//to check if scripts should be running or not
function chkAuthentication(){
	//alert(PFFormMode);
	if (PFFormMode == "EDIT")
		 return true;
	else
		return false;
}

//to check if scripts should be running or not
function chkAuthentication1(){
	alert(PFFormMode());
	return true;
	alert(document.referrer);
	if (document.referrer.indexOf("User") > -1)
		 return true;
	else
		return false;
}

function clearImage(fldName){
   var fld = getFieldElement(fldName);
   fld.value = "";
   if (fldName.indexOf("Award") >= 0){
      fldText = fldName.substr(0,fldName.lastIndexOf("Constant")) +
         "TextConstant";
      if (getFieldElement(fldText))
         getFieldElement(fldText).value = "";

      shiftAwards();      
   }
   refreshPage();
}

function createClearButton(fldName){
   if (document.getElementById("btnClr"+fldName) == null && getElementValue(fldName) != "")
      return("<a id='btnClr"+fldName+"' class='controls'>" + 
         "<span class='button' onclick='clearImage(\""+fldName+"\")' " +
         "onMouseOver=\"this.className='buttonRollover';\" " +
         "onMouseOut=\"this.className='button';\" >" +
         "<nobr>Clear</nobr></span></a>");
   else
      return "";
}

function createClearButton1(fldName){
   if (document.getElementById("btnClr"+fldName) == null && getElementValue(fldName) != "")
      return("<a id='btnClr"+fldName+"' class='controls'>" + 
         "<span class='button' onclick='"+new Function ("clearImage(\""+fldName+"\");") +"' "+
         "onMouseOver=\"this.className='buttonRollover';\" " +
         "onMouseOut=\"this.className='button';\" >" +
         "<nobr>Clear</nobr></span></a>");
   else
      return "";
}

function createSelectButton(side, daCode, fldName, btnTitle, defCat)
{
    if (document.getElementById("btnSel"+fldName) != null)
        return "";
   var btn="<p>" + btnTitle + "</p>" + 
      "<a id=\"btnSel"+fldName+"\" class='controls'>" + 
      "<span class='button' onClick=\"popupWindow('" + side + "'," +
      daCode + ",'" + fldName + "','" + defCat + "')\" " +
      "onMouseOver=\"this.className='buttonRollover';\" " +
      "onMouseOut=\"this.className='button';\" >" +
      "<nobr>Select...</nobr></span></a>";
   return btn;
}

function createSelectButton1(side, daCode, fldName, btnTitle, defCat)
{
    if (document.getElementById("btnSel"+fldName) != null)
        return "";

   var btn="<a id=\"btnSel"+fldName+"\" class='controls'>" + 
      "<span class='button' onClick=\"popupWindow('" + side + "'," +
      daCode + ",'" + fldName + "','" + defCat + "')\" " +
      "onMouseOver=\"this.className='buttonRollover';\" " +
      "onMouseOut=\"this.className='button';\" >" +
      "<nobr>" + btnTitle + "</nobr></span></a>";
   return btn;
}

function disableEnable(fldName, flag){
   //flag should be "disabled" (true) or "" (false).
   getFieldElement(fldName).disabled = flag;
}

function disableFld(fldName){
   disableEnable(fldName, DISABLE);
}

function enableFld(fldName){
   disableEnable(fldName, ENABLE);
}

/* To check if the field exists in the project or not.
*/
function fieldExist(fldName){
	if (getFieldElement(fldName) == null)
	   return false;
	else
	   return true;
}

function getDivElement(fldName){
   return document.getElementById("DIV_" + FieldIDs[fldName]);
}

function getElementValue(fldName){
return PFSF_GetFieldValueByName("FIELD_" + FieldIDs[fldName]);
	/*var fld = getFieldElement(fldName);
	if (fld)
   	return fld.value;
   else
   	return "";*/
}


function getFieldElement(fldName){
var c1 = document.getElementById("FIELD_" + FieldIDs[fldName]);
//var c2 = PFSF_Find('FIELD_' + FieldIDs[fldName]);
return c1;
}


function hideFld(fldName){
   showHide(fldName, HIDE);
}

/* To hide My Profile menu item from the page.
*/
function hideMyProfile(){
   var ctrl = document.getElementById("_ctl0_profile_anchor");
   if (ctrl == null)
	   ctrl = document.getElementById("_ctl1_profile_anchor");
   ctrl.style.display = HIDE;
   ctrl.parentNode.previousSibling.style.display = HIDE;
}

function insertErr(errMsg, fldName){
	//for debugging purposes only.
	if (false){
		var v = getDivElement(fldName);
		var s="";
		for (var i=0; i<v.childNodes.length; i++)
			s+=i+"-->"+v.childNodes[i].innerHTML+"\n";
		alert(s);
	}
	
   var fld = getDivElement(fldName).childNodes[0];
   var fldTitle = fld.innerHTML;
   fldTitle = fldTitle.substr(0,fldTitle.indexOf(":")+1) + " " +
      '<span class="mySpan">' + errMsg + "</span>";
   fld.innerHTML = fldTitle;
   //if (errMsg != "")
      //getFieldElement(fldName).focus();
}

function insertErrWithFlag(errMsg, fldName){
   if (errMsg != "")
      errInPage = true;

   insertErr(errMsg, fldName);
}

function PFSF_getFieldValue(fldName){
   /* This func works well with Radio buttons.
   /* using .value property doesn't work properly for this case.
   /*/
   return PFSF_GetFieldValueByName("FIELD_"+FieldIDs[fldName]);
}

function popupWindow(side, productType, imgField, defCat){
   if (productType == DA_LAYOUT)
      var hideLib = 1;
   else
      var hideLib = 0;
      
   if (!defCat || defCat == "undefined")
      defCat = "";

   popupType = productType;
   var param = "http://" + document.domain +
      '/dam/chooseimage3.aspx?xat=0&side='+side+'&dep='+DA_DEP+
      '&productType='+productType+'&imgField='+imgField+'&defCat='+defCat+
      '&hideMyLibrary='+hideLib;
    
   if (productType == DA_AWARD)
      param += '&filter=AwardGroup_'+awardGrp;
   else if (imgField == "BGBackConstant")
      param += '&catId=10';

   //var win = window.open(param,'popup','fullscreen=yes,scrollbars,resizable');
   var win = window.open(param,'popup','fullscreen=yes,scrollbars');
   win.focus();
}

function refreshPage(){
   switch (prdctType){
      case "H":      //same thing performs as for "V"
      case "V":
         fixContacts();
         break;
      case "H2P":
         fixContacts2();
   }
   
   window.document.forms[0].submit();
   //PFSF_AjaxUpdateForm();
   //window.__doPostBack('btnPreview','');
   /* The following statement also works in IE but not in Netscape.
   /* This is better, cause I can perform additional tasks in Hook func.
   /*/
   //document.getElementById("btnPreview").click();
}

/* to be called when user clicks on next step arrow
*/
function setNextClicked(){
   nextClicked = true;
}

function showFld(fldName){
   showHide(fldName, SHOW);
}

function showHide(fldName, flag){
if (getDivElement(fldName) != null) //alert("Error finding field: " + fldName);
   getDivElement(fldName).style.display = flag;
}

function specialHide(fldName){
   specialShowHide(fldName, HIDE);
}

function specialShow(fldName){
   specialShowHide(fldName, SHOW);
}

function specialShowHide(fldName, flag){
   document.getElementById(fldName).style.display = flag;
}

function swap(fld1, fld2){
   if (fld1 && fld2){
      var tmp = fld1.value;
      fld1.value = fld2.value;
      fld2.value = tmp;
   }
}

function updateCurrentPageNum(){
   PFSF_SetControlValue(PFSF_Find('FIELD_' + FieldIDs['CurrentPage']), 
      getFirstVisiblePage());
   PFSF_ShowHideConditionalFields();
}

/* these variables are public and used by lots of functions.
/*/
var SHOW = "block", HIDE = "none";
var DISABLE = true, ENABLE = false;

var BG_COLOR = "FFFFA0";		// previously "LemonChiffon"
var nextClicked = false;

//window.onload = Init;
/* to run scripts after document is loaded.*/

if ( typeof window.addEventListener != "undefined" )
    window.addEventListener( "load", Init, false );
else if ( typeof window.attachEvent != "undefined" )
    window.attachEvent( "onload", Init );
else {
    if ( window.onload != null ) {
        var oldOnload = window.onload;
        window.onload = function ( e ) {
            oldOnload( e );
            Init();
        };
    }
    else
        window.onload = Init;
}

